472,811 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,811 software developers and data experts.

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 6457
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: James Harriman | last post by:
Hi, I need to be able to measure a time interval in milliseconds on a windows machine. I have tried using time.clock() but it appears to measure time in seconds...Is there a way to measure time...
2
by: arcvonz | last post by:
We have a time critical smart client application running on Windows 2000/XP systems. Is there any reliable service that would help in syncing the local system time to atomic clock or local time at...
5
by: Dica | last post by:
i need to create a clock object which is apparently in the clock class under System.Windows.Media.Animation. i've tried adding a reference to this assembly, but the closest thing i can find is...
10
by: MLH | last post by:
I wish to give someone an A97 database they can install on their PC as a runtime application. I want them to click a button on a form (frmDocumentMyPC). I want to document as much useful...
12
by: jUrner | last post by:
Hello all I have the problem of how to calculate the resolution of the system clock. Its now two days of head sratching and still there is nothing more than these few lines on my huge white...
30
by: Matt | last post by:
Does clock_t clock() measure real time or process time? Unless I have missed something, Stroustrup's treatment in TC++PL (section D.4.4.1) is none too clear on that question. Is clock()...
2
by: s.storrow | last post by:
I'm trying to write a piece of software in C++ which will display a clock in the windows system tray. I am able to create icons there, but i am not sure whether it is possible to write text there,...
2
by: Dica | last post by:
i'm working on a windows service that needs to check the system's clock every minute and see if there are any jobs in my database taht are scheduled to run at that time: sSql = "select * from...
9
by: Ron Adam | last post by:
I'm having some cross platform issues with timing loops. It seems time.time is better for some computers/platforms and time.clock others, but it's not always clear which, so I came up with the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.