![]() |
|
#1
|
||||
|
||||
rand() giving the same valueHey, I have a program that I'll be posting in the Source Code/Tutorials section, and I'm having a slight problem with it. It uses the rand() function from cstdlib to get a random number between 1 and 4 to determine which direction to move a distance of one unit. Here's the problem code:
CPP / C++ / C Code:
The point datatype consists of just two integers named x and y. Now... I'm really wondering why I'm only getting the number three when I should be getting random numbers between 1 and 4. |
|
#2
|
||||
|
||||
|
Aaron, you need to seed the random generator with a call to srand(int). Take a look at this thread and its links for a complete discussion on the rand() function.
rand() is decidedely unrandom. For that reason, a seed of the current time is used often times to give the appearance of randomness. For what you are trying to do though you could probably just throw it a static int. The pattern will always be the same though. |
|
#3
|
||||
|
||||
|
The seed! Of course! I forgot about that. Could you possibly show me how to get the current time so that I could seed the rand() function with it?
EDIT: Nevermind, I found it in the thread you referred me to. Thanks : ) |
Recent GIDBlog
I?m Home by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The