GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Apr-2006, 10:39
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

? regarding random number generation


Here's something that has bugged me for a while. I know how to seed the random function via the current time:
CPP / C++ / C Code:
#include <ctime>

srand (time(NULL));
The problem that I have with this is that the randomness changes only once per second with this method, which is not truly random. Is there another method that changes the seed value more often per second, or is there not a standard function that does this?
  #2  
Old 09-Apr-2006, 20:45
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: ? regarding random number generation


you could use the clock() function, which outputs the number of clock ticks since process start. The (slight) problem is that this can be the same on two different instances of the process (unlikely). To remedy that, you could combine time(NULL) and clock(), by addition or multiplication or something.
  #3  
Old 09-Apr-2006, 20:50
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Re: ? regarding random number generation


Quote:
Originally Posted by ubergeek
you could use the clock() function, which outputs the number of clock ticks since process start.

Sorry for my ignorance, but when you say "the number of clock ticks", is the clock ticking faster than once per second? If not, the question remains.
  #4  
Old 09-Apr-2006, 20:56
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: ? regarding random number generation


short answer: yes.

medium-length answer: yes, 1000 times per second in fact.

long answer: allow me to cite a few pertinent quotes
Quote:
Originally Posted by http://www.cplusplus.com/ref/ctime/
CLK_TCK
Constant that defines the number of clock ticks per second. Used by clock function.
Quote:
Originally Posted by C:\Dev-Cpp\include\time.h
#define CLOCKS_PER_SEC ((clock_t)1000)
#define CLK_TCK CLOCKS_PER_SEC
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting a number amount to text Godzilla C++ Forum 5 31-Mar-2006 11:38
Need help generating a random number Allie C Programming Language 7 09-Nov-2005 22:18
random number generation issues Jonnyz007 C++ Forum 8 27-Oct-2005 19:13
Need Help with my Cards Program (C++) krisopotamus C++ Forum 2 06-Oct-2005 16:48
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 20:25

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:31.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.