Saturday, May 22, 2010

Can anyone tell me how to use the rand function in C?

I use it but everytime i start the program again its in the same place.

Can anyone tell me how to use the rand function in C?
Rand() always produces the same sequence unless you first "seed" the function. That is done with srand().
Reply:The reason you're seeing the same series of random numbers is because the rand function has not been initialized. In order for rand to generate random numbers that differ from one execution to another, you should call the randomize() function at the start of your program.





Be sure not to make the rookie program mistake of calling randomize over and over. It only needs to be called once to seed the rand function.





http://phanderson.com/C/random.html


No comments:

Post a Comment