LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   random numbers (https://www.linuxquestions.org/questions/programming-9/random-numbers-296005/)

deveraux83 02-28-2005 05:16 PM

random numbers
 
Hi all,

I was just wondering how I would go about generating a sequence of random numbers that don't repeat each time the program restarts. rand() always repeats the exact same sequence each time the program is re-run and I was wondering how I might change that.

I remember some time ago (at least 4 years) when there was a function called 'randomize' that made sure that the sequences were not repeated but I can't seem to find this function in c++ in linux.

Any help is appreciated.

itsme86 02-28-2005 05:30 PM

Add srand(time(0)); to your program before any calls to rand(). You'll need to #include <time> and make sure you only call srand() one time. I usually add that line just after declaring variables in main().

deveraux83 02-28-2005 05:36 PM

cool! thanks a lot.


All times are GMT -5. The time now is 01:14 AM.