# Thread: Problem with random numbers

1. ## Problem with random numbers

So I made a function that creates a random number between the desired numbers. It works out all fine but when you call the function for a second time, the recieve the same number again and again. This is the function here:

Code:
```int random(int LOW, int HIGH, int VALUE)
{
time_t seconds;
time(&seconds);
VALUE = rand() % (HIGH - LOW + 1) + LOW;
return (VALUE);
}```
So what am I doing wrong?

2. don't you call by chance srand before every call of your function?

3. Originally Posted by vart
don't you call by chance srand before every call of your function?
Umm... I don't understand.

4. > time(&seconds);
What happens after this?
You're not even using seconds, so by bother reading the time, if you're not going to use the result?

vart just took a guess that the line of code you omitted from your post is something like
srand( seconds );

Which, if you call the same function many times in the same second, will return the same result every time.

5. You should be able to drop the time bit completely out your random number generator. Heres one I made. Basically, like vart said, you only need to seed the randomization once. If you repeatedly seed it in a loop it will have the reverse effect.
Code:
```int Rand(int lo, int hi){return lo+(rand() % (hi-lo+1));}

int main()
{
srand(time(NULL)); //Seed randomisation once at the start of the prog
int i;
for(i=1;i<=30;i++) cout << Rand(2, 10) << "\n";
cin >> i;
return 0;
}```

6. I think yaya needs to be eternally confuzzled by Prelude:
Random Numbers Tutorial
Using rand()

7. Originally Posted by yaya
So what am I doing wrong?
You're calling time(). This makes me terribly suspicious that you are also calling srand() but you haven't posted it.

You have a pointless argument called VALUE. It is needless, get rid of it.

Popular pages Recent additions