| | |
GetTickCoun(), rand() are they the same?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2004
Posts: 16
Reputation:
Solved Threads: 0
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
•
•
Join Date: Nov 2004
Posts: 108
Reputation:
Solved Threads: 3
The reason rand() generated the same numbers each time is that you have to seed the random number generator
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <ctime> using namespace std; int main() { int i; //seeds the number generator with time srand((unsigned int)time(NULL));//you only need to call this once in your program for(i = 0; i < 10; i++); { cout<<rand()<<endl; } return 0; }
Join me on IRC:
Server: irc.daniweb.com
Channel: #C++
Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
Server: irc.daniweb.com
Channel: #C++
Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
•
•
Join Date: Sep 2004
Posts: 10
Reputation:
Solved Threads: 0
Adding on, GetTickCount() is also a way to seed the random number generator
C++ Syntax (Toggle Plain Text)
srand( GetTickCount() );
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).
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).
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: edlin text editor
- Next Thread: Help with error message
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





