GetTickCoun(), rand() are they the same?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2004
Posts: 16
Reputation: nico is an unknown quantity at this point 
Solved Threads: 0
nico nico is offline Offline
Newbie Poster

GetTickCoun(), rand() are they the same?

 
0
  #1
Jan 31st, 2005
Is there a diffrence between the GetTickCount() and rand() functions ? I read they both are Psuedorandom number generators , I would like to know if there is any diffrence between those two functions , I used rand() once and the output was the same each time the program run , would that change if i use GetTickCount instead ? thankyou very much
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 108
Reputation: prog-bman is an unknown quantity at this point 
Solved Threads: 3
prog-bman prog-bman is offline Offline
Junior Poster

Re: GetTickCoun(), rand() are they the same?

 
0
  #2
Jan 31st, 2005
The reason rand() generated the same numbers each time is that you have to seed the random number generator
  1. #include <iostream>
  2. #include <ctime>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9. int i;
  10. //seeds the number generator with time
  11. srand((unsigned int)time(NULL));//you only need to call this once in your program
  12. for(i = 0; i < 10; i++);
  13. {
  14. cout<<rand()<<endl;
  15. }
  16.  
  17. return 0;
  18. }
Join me on IRC:
Server: irc.daniweb.com
Channel: #C++

Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 10
Reputation: Emmitt310 is an unknown quantity at this point 
Solved Threads: 0
Emmitt310 Emmitt310 is offline Offline
Newbie Poster

Re: GetTickCoun(), rand() are they the same?

 
0
  #3
Feb 2nd, 2005
Adding on, GetTickCount() is also a way to seed the random number generator

  1. srand( GetTickCount() );
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 16
Reputation: nico is an unknown quantity at this point 
Solved Threads: 0
nico nico is offline Offline
Newbie Poster

Re: GetTickCoun(), rand() are they the same?

 
0
  #4
Feb 2nd, 2005
thanx for the reply guys , i really appreciate it
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: GetTickCoun(), rand() are they the same?

 
0
  #5
Feb 2nd, 2005
And GetTickCount() should be something like (depending on the system) the number of 'ticks' since the machine booted. On Windows it it milliseconds since boot, and wraps around every month or two (assuming Windows can stay up that long!).

So it is 'random' in the sense that it will be different, but it is a number starting at 1 and growing every millisecond. And if you sample it twice quickly it will be the exact same value.

rand is defined as always returning the same sequence given the same seed so that you can test out your program. That is, every time your program runs it will get the same set of 'random' values so you can fix bugs and the like. Then you add srand() to give it a different seed (with time() or ticks or sampling the time deltas between keystrokes or whatever).
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 16
Reputation: nico is an unknown quantity at this point 
Solved Threads: 0
nico nico is offline Offline
Newbie Poster

Re: GetTickCoun(), rand() are they the same?

 
0
  #6
Feb 3rd, 2005
thanx chainsaw
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC