By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,929 Members | 631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,929 IT Pros & Developers. It's quick & easy.

Read System Clock in Windows

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a

"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.