Read System Clock in Windows

M

Moikel

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
 
R

Richard Heathfield

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

Barry Schwarz

Moikel said:


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
 
R

Rod Pemberton

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. 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/default.asp?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
 
S

Skarmander

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

QueryPerformanceCounter()
http://msdn.microsoft.com/library/default.asp?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.
 
R

Rod Pemberton

Skarmander said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top