Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

rand()

Status
Not open for further replies.

Taxidriver

Programmer
Jan 15, 2002
79
IT
Hello, I have a code that, given n segments in input, randomizes their order. The segments are stored in a linked list. Every element of the list is swapped with a random element calculated with the function rand(). the problem is that every time I run the program, the function rand() returns always the same numbers, in the same order! For example, 4 segments are always permuted following the sequence 1-3-2-0.
How can I have a better randomization?
thanks.
 
You have to seed the random number generator. Take a look at your documentation for srand(). ______________________________________________________________________
Never forget that we are
made of the stuff of stars
 
You can seed the random-number generator "rand()" with the current time.

#include <time.h>
....
..
srand((unsigned )time( NULL ));
...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top