#### Archived

This topic is now archived and is closed to further replies.

# Why wont the random number be random?

This topic is 5992 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

since i never use random number( until now) im completely stuck with this little prob when i do rand() % 10 the number that comes up stays there forever so if i do a second random number its the same as the first! can u plz tell me how to make it random every time thx

##### Share on other sites
You need to call srand() once at the start of your program.

##### Share on other sites
just srand()? no srand with something in the parenthesis?

##### Share on other sites
use:

srand((unsigned int)time(NULL));

This will seed the pseudo-random number generator with the system time.

Note that you must also include the headers time.h and stdlib.h.

Edited by - Tab on September 22, 2001 1:41:02 PM

##### Share on other sites
will that give me a number between 0 and the time?

if so that wont do cuz i need a number from 0-1000 often times

for the header you mean time.h?

##### Share on other sites
First include time.h. Then type this in your main function before do any random things: srand(time(NULL));

Then you can do the rand()%10 which would give you a random number from 0 to 9.

Edited by - IronFist on September 22, 2001 1:43:10 PM

##### Share on other sites
why would i put it in the main function when im making a random number in several other classes?

should i put srand in the header files with the classes?

##### Share on other sites
Hmmm. Thats a good question. I am not quite sure. Hopefully someone else can tell you. But if I were you, I would just experiment with it. Try putting the srand function in the header files with your classes like you said, and if that doesn''t work, try putting it somewhere else.

##### Share on other sites
hehe thx any man
thx to all of you, now i can continue with my game

##### Share on other sites
hehe thx any way man
thx to all of you, now i can continue with my game

##### Share on other sites
Put it *first* thing in main so that it''ll be the first thing that happens in your program.

It won''t give you a number from 0-time because it just uses time as a semi-random number so that you can generate more random numbers from it. Time is usually a big, complex, unpredictable number, thus making it a good candidate for random numbers. You can have any number you want after that; from 0-(however big long gets) by doing this:

//srand before thisrand()%x;//where x is the biggest number you want.

K? If nothing happens before main() then do srand in the first line of main. Otherwise...I dunno, you could probably do it in a constructor for a class, then declare the class (globally) before all your other global variables and that would make *sure* that you don''t have problems like this.

Note: Don''t call srand() in a loop as you get random variables ever. This will cause the same problem that you had in the beginning because the time numbers will be too close together.

-> www.scallion.sp00fed.net <-

##### Share on other sites
Put the call to srand() in your main() function, and make sure it gets called before you instantiate any of the classes that call rand(). That way, you''re sure the random number generator has been properly seeded.

aight thx guys

##### Share on other sites
and if you wanted the random numbers between 5 and 9 you''d do this:

(rand()%5)+5

A CRPG in development...

Need help? Well, go FAQ yourself.

##### Share on other sites
many thanks to you americans

##### Share on other sites
And you should also avoid calling srand more than once in your program, as that will reseed the random-number generator.

-Neophyte

- Death awaits you all with nasty, big, pointy teeth. -

##### Share on other sites
Actually, that can often be an effective technique.

~~~~~~~~~~
Martee