I created a slot machine program which runs ok, but every time I run the program i get the same numbers randomized - could someone please tell me what i am doing wrong...by the way I am using a = 1+rand() %10 (is there a better way)
Printable View
I created a slot machine program which runs ok, but every time I run the program i get the same numbers randomized - could someone please tell me what i am doing wrong...by the way I am using a = 1+rand() %10 (is there a better way)
Uhh that way will allways give you the same number when you run it!
use this I used it in my game also:
and then when you need a random number do:Code:int random(int low, int high)
{
srand(time(NULL));
return low + rand()%(high-low+1);
}
Hope that helpsCode:random (5,15); //a random number between 5 and 15
-Devouring One-
You can use srand(time(0));
and don't forget to include <ctime>
Ohh yeah i forgot to say that sorry... :D
Eh, no! Only call srand(time(NULL)) once in your program. Not every time you retrieve a random number.Quote:
Originally posted by devour89
Code:int random(int low, int high)
{
srand(time(NULL));
return low + rand()%(high-low+1);
}
I said that. Read it again:
and then when you need a random number do:
Code:random (5,15); //a random number between 5 and 15
If you do this:Quote:
Originally posted by devour89
I said that. Read it again:
and then when you need a random number do:
Code:random (5,15); //a random number between 5 and 15
You call Random(...) 5 times, and thus calling srand() 5 times. Not good, you should only call srand() 1!!! time in your program.Code:int main()
{
for(int i=0; i<5; i++)
cout << Random(0, 10) << endl;
return 0;
}
Remove it from the random function and place it in the beginning of your program instead.
seeding the generator at every call of the random function leads to a repeating pattern.. that's why you don't want to do it.