Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

time in milliseconds/c++

915 views
Skip to first unread message

Katharine Chartrand

unread,
Feb 3, 2002, 8:29:13 PM2/3/02
to
Is there a way to get the time in milliseconds in c++? The time() function
gives seconds, but my program calls the random number generator more than
once per second.

Thanks,
>KNC<


Jem Berkes

unread,
Feb 3, 2002, 8:48:15 PM2/3/02
to
> Is there a way to get the time in milliseconds in c++? The time()
> function gives seconds, but my program calls the random number
> generator more than once per second.

timeGetTime() has good resolution

--
http://www.pc-tools.net/
Windows, Linux & UNIX software

Tim Robinson

unread,
Feb 3, 2002, 8:52:36 PM2/3/02
to
"Katharine Chartrand" <k...@usa.com> wrote in message
news:Jtl78.14275$Ri2.46976@rwcrnsc54...

| Is there a way to get the time in milliseconds in c++? The time() function
| gives seconds, but my program calls the random number generator more than
| once per second.


Whoa... why does the *clock* have any bearing on the *random number
generator*?

Are you doing this, in an attempt to get really random numbers?

for (;;)
{
srand(time(NULL));
printf("%d %d %d %d\n", rand(), rand(), rand(), rand());
}

If so, don't. Don't initialise the random number generator more than once
because, as you've seen, the numbers get *less* random (and start depending
on the clock).

To answer your question directly: there's no standard C or C++ way to do it,
but Windows has the GetTickCount() function with a resolution of 55ms or
finer.

--
Tim Robinson
http://www.themoebius.org.uk/

Jerry Coffin

unread,
Feb 4, 2002, 2:51:12 AM2/4/02
to
In article <Jtl78.14275$Ri2.46976@rwcrnsc54>, k...@usa.com says...

> Is there a way to get the time in milliseconds in c++? The time() function
> gives seconds, but my program calls the random number generator more than
> once per second.

You should be seeding the random number generator ONCE when the
program starts up. After that, the clock isn't normally involved in
generating random numbers at all.

--
Later,
Jerry.

The Universe is a figment of its own imagination.

Michael Daniloff

unread,
Feb 4, 2002, 3:11:32 AM2/4/02
to
"Katharine Chartrand" <k...@usa.com> wrote in message news:<Jtl78.14275$Ri2.46976@rwcrnsc54>...


Win32 API function GetSystemTime() takes a pointer to SYSTEMTIME as a parameter.
One of the fields in this structure specifies current milliseconds.

Regards,

Michael

Dea

unread,
Feb 5, 2002, 6:43:04 PM2/5/02
to
Yes, use the multimedia timer ( timeGetTime() ). This will give you
1millisecond resolution. The GetSystemTime() will not give you this.

Demir


"Katharine Chartrand" <k...@usa.com> wrote in message
news:Jtl78.14275$Ri2.46976@rwcrnsc54...

0 new messages