# Random Number

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 07-19-2002
Unregistered
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?
• 07-19-2002
The Dog
Quote:

a better method
Mmmmmm............
Perhaps you're doing something wrong!
• 07-19-2002
Hammer
Do a board search, there's a whole load of posts on this topic...
• 07-19-2002
endo
That should work pretty good for random numbers, you could post more of your code so someone can check it.
• 07-19-2002
Hammer
Here's a quote from Prelude in another thread:
Quote:

>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
• 07-19-2002
quzah
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.
• 07-19-2002
Hammer
>In this case, you'll need to use 'srand' to seed your random number generator.
Quote:

From the first post...
I've already tried to generate random numbers this way: srand((unsigned) time(NULL));
variable = rand()%30;
:)
• 07-19-2002
golfinguy4
quzah is saying that s/he needs to reseed before each call to rand(). This should work by the way.
• 07-19-2002
Hammer
Quote:

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.
• 07-19-2002
quzah
Quote:

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...

Quote:

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.
• 07-19-2002
Unregistered
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.
• 07-19-2002
Hammer
Quote:

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?
• 07-19-2002
quzah
Quote:

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

Quzah.
• 07-19-2002
Unregistered
so where do I put the srand() in my code?
• 07-19-2002
quzah
Quote:

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.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last