1. ## Random Number

I've already tried to generate random numbers this way: srand((unsigned) time(NULL));
variable = rand()%30;

but if i tried getting multiple random numbers, I always got the same ones. Can someone give me a better method for generating a random number?

2. a better method
Mmmmmm............
Perhaps you're doing something wrong!

3. Do a board search, there's a whole load of posts on this topic...

4. That should work pretty good for random numbers, you could post more of your code so someone can check it.

5. Here's a quote from Prelude in another thread:
>well im triing to get random numbers between say 23 and 35.
r = ( rand() * ( 35 - 23 ) ) / RAND_MAX + 23;

>if i use rand()%35 than it picks numbers between 0 and 35.
Small ranges like that are frighteningly not random, so you'd best stick with the code I gave above. It uses the high order bits of a value instead of the low order bits which your method uses.

-Prelude

6. It could be that when they say "when I do it 10 times, it's the same", they really mean they're running the program 10 times and always getting the same number.

In this case, you'll need to use 'srand' to seed your random number generator.

If you'd have read the FAQ you'd know this.

Quzah.

7. >In this case, you'll need to use 'srand' to seed your random number generator.
From the first post...
I've already tried to generate random numbers this way: srand((unsigned) time(NULL));
variable = rand()%30;

8. quzah is saying that s/he needs to reseed before each call to rand(). This should work by the way.

9. Originally posted by golfinguy4
quzah is saying that s/he needs to reseed before each call to rand(). This should work by the way.
I believe that would be incorrect.

10. Originally posted by Hammer
>In this case, you'll need to use 'srand' to seed your random number generator.

My bad. That's what happens when you post while talking on the phone...

uzah is saying that s/he needs to reseed before each call to rand(). This should work by the way.
No. This is not what I meant because this is incorrect.

Quzah.

11. Depending on what application you want to use your random number generator for, you might try downloading a better random number generator that I wrote. You can find it at:

http://www.planetsourcecode.com/vb/s...=4385&lngWId=3

This random number generator outputs random numbers with a perfectly uniform distribution. This might be bad for your purposes, but in combination with the modulus operator you should be able to manipulate the distribution a little.

My other suggestion is that instead of seeding the traditional random number generator directly from the clock, you might try the stdlib.h function randomize(). I'm not sure exactly what that function does, but it works pretty well for me.

12. Originally posted by Unregistered
My other suggestion is that instead of seeding the traditional random number generator directly from the clock, you might try the stdlib.h function randomize(). I'm not sure exactly what that function does, but it works pretty well for me.
Is randomize a standard function, or is it compiler specific... I can't see it in the standard reference sites?

13. Is randomize a standard function, or is it compiler specific...
In a word, Borland.

Quzah.

14. so where do I put the srand() in my code?

15. Originally posted by Unregistered
so where do I put the srand() in my code?
I take it someone has never read the FAQ? You need to call srand one time only, before you ever call rand. So basicly just call it right after you declare your variables in main. Or hell, since you're using C++, just do:
Code:
```int main( )
{
srand( time(0) );

...my stuff here...

return 0;
}```
Since C++ doesn't care where you declare variables, just call srand first.

Quzah.