Posted by Dan Nuffer on 1999-09-18
> Is there a way to use random in C++? > I tried rand() but I keep getting the same sequence > every time I run the program,I would like the program > to give different values everytime I run the program. > for example first time I run the program I get the > values lets say 1,44,22,34 > then the second time I run the program I get lets say > 33,11,55,88,100 > thanks in advanced rand() produces a reproducible random number sequence, given the same seed value. So every time you run your program if you want to get a difference sequence of random numbers, you must initialize rand() with a different seed. One common way of doing this is by getting the current time and using that as the seed. Like this: srand( (unsigned)time( NULL ) ); Just put this line in the beginning of your program and you will get the results you desire. Dan Nuffer dan@xxx.xxx http://students.cs.byu.edu/~nuffer Get PAID to surf the web http://www.alladvantage.com/go.asp?refid=AVX481
Previous post | Next post | Timeline | Home