943,483 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3574
  • C++ RSS
Jan 31st, 2005
0

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

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nico is offline Offline
16 posts
since Nov 2004
Jan 31st, 2005
0

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

The reason rand() generated the same numbers each time is that you have to seed the random number generator
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 14
Solved Threads: 4
Junior Poster
prog-bman is offline Offline
108 posts
since Nov 2004
Feb 2nd, 2005
0

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

Adding on, GetTickCount() is also a way to seed the random number generator

C++ Syntax (Toggle Plain Text)
  1. srand( GetTickCount() );
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Emmitt310 is offline Offline
10 posts
since Sep 2004
Feb 2nd, 2005
0

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

thanx for the reply guys , i really appreciate it
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nico is offline Offline
16 posts
since Nov 2004
Feb 2nd, 2005
0

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

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).
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Feb 3rd, 2005
0

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

thanx chainsaw
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nico is offline Offline
16 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: edlin text editor
Next Thread in C++ Forum Timeline: Help with error message





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC