470,571 Members | 2,299 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,571 developers. It's quick & easy.

Read System Clock in Windows

Hello,

I'm writing a program that requires that I generate random numbers. I'm
using the C rand()function. I want to use the system clock as a seed
for the function. How do I read the system clock in Windows? (Please
state any libraries I'm going to need etc).

Thanks,

Moikel

Jul 14 '06 #1
5 6296
Moikel said:
Hello,

I'm writing a program that requires that I generate random numbers. I'm
using the C rand()function. I want to use the system clock as a seed
for the function.
http://c-faq.com/lib/srand.html
How do I read the system clock in Windows?
time()
(Please
state any libraries I'm going to need etc).
The standard library, which is linked in by default so you don't need to
worry about it.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 14 '06 #2
On Fri, 14 Jul 2006 22:27:28 +0000, Richard Heathfield
<in*****@invalid.invalidwrote:
>Moikel said:
>Hello,

I'm writing a program that requires that I generate random numbers. I'm
using the C rand()function. I want to use the system clock as a seed
for the function.

http://c-faq.com/lib/srand.html
>How do I read the system clock in Windows?

time()
>(Please
state any libraries I'm going to need etc).

The standard library, which is linked in by default so you don't need to
worry about it.
But the OP should be concerned about including the correct header.
Remove del for email
Jul 15 '06 #3

"Moikel" <ob*************@gmail.comwrote in message
news:11*********************@75g2000cwc.googlegrou ps.com...
Hello,

I'm writing a program that requires that I generate random numbers. I'm
using the C rand()function. I want to use the system clock as a seed
for the function. How do I read the system clock in Windows? (Please
state any libraries I'm going to need etc).
Yahoo'd for the 'rdtsc' instruction and Windows:

QueryPerformanceCounter()
http://msdn.microsoft.com/library/de...?url=/library/
en-us/winui/winui/windowsuserinterface/windowing/timers/
timerreference/timerfunctions/queryperformancecounter.asp
You could also do something along these lines:

unsigned long long rdtsc(void)
{
unsigned long long cycles=0;
#ifdef __DJGPP__
__asm__ __volatile__(
"rdtsc\n"
:"=A"(cycles)
);
#endif
#ifdef __WATCOMC__
_asm {
rdtsc
mov dword ptr [cycles], eax
mov dword ptr [cycles+4], edx
}
#endif
return cycles;
}

The DJGPP code should work for GCC compilers. The OpenWatcom code should
work for MS compilers. If not, they are close...
Rod Pemberton

Jul 15 '06 #4
Rod Pemberton wrote:
"Moikel" <ob*************@gmail.comwrote in message
news:11*********************@75g2000cwc.googlegrou ps.com...
>Hello,

I'm writing a program that requires that I generate random numbers. I'm
using the C rand()function. I want to use the system clock as a seed
for the function. How do I read the system clock in Windows? (Please
state any libraries I'm going to need etc).

Yahoo'd for the 'rdtsc' instruction and Windows:

QueryPerformanceCounter()
http://msdn.microsoft.com/library/de...?url=/library/
en-us/winui/winui/windowsuserinterface/windowing/timers/
timerreference/timerfunctions/queryperformancecounter.asp
This is not the system time, this is the value of the processor-specific
high resolution timer (if it exists). Using this as a seed for the C random
number generator is massive overkill, and it makes your program gratuitously
unportable.

How would this ever be more appropriate than time()? I can imagine some
things you need high resolution timing for, but seeding a RNG isn't one of
them -- and if you do need that for some reason, you should probably use
your own RNG in the first place, rather than use rand() (about which the
standard guarantees next to nothing).

S.
Jul 15 '06 #5

"Skarmander" <in*****@dontmailme.comwrote in message
news:44***********************@news.xs4all.nl...
Rod Pemberton wrote:
"Moikel" <ob*************@gmail.comwrote in message
news:11*********************@75g2000cwc.googlegrou ps.com...
Hello,

I'm writing a program that requires that I generate random numbers. I'm
using the C rand()function. I want to use the system clock as a seed
for the function. How do I read the system clock in Windows? (Please
state any libraries I'm going to need etc).
Yahoo'd for the 'rdtsc' instruction and Windows:

QueryPerformanceCounter()
http://msdn.microsoft.com/library/de...?url=/library/
en-us/winui/winui/windowsuserinterface/windowing/timers/
timerreference/timerfunctions/queryperformancecounter.asp

This is not the system time, this is the value of the processor-specific
high resolution timer (if it exists). Using this as a seed for the C
random
number generator is massive overkill, and it makes your program
gratuitously
unportable.

How would this ever be more appropriate than time()? I can imagine some
things you need high resolution timing for, but seeding a RNG isn't one of
them -- and if you do need that for some reason, you should probably use
your own RNG in the first place, rather than use rand() (about which the
standard guarantees next to nothing).
Sorry, so true. I was a bit asleep, and only remember reading "How do I
read the system clock in Windows?"...

RP
Jul 15 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by James Harriman | last post: by
2 posts views Thread by arcvonz | last post: by
5 posts views Thread by Dica | last post: by
12 posts views Thread by jUrner | last post: by
30 posts views Thread by Matt | last post: by
2 posts views Thread by s.storrow | last post: by
9 posts views Thread by Ron Adam | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.