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)
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-
Dev C++
Win XP/2k/98
I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!
He's lean he's keen... He's the spank machine!
You can use srand(time(0));
and don't forget to include <ctime>
none...
Ohh yeah i forgot to say that sorry...
Dev C++
Win XP/2k/98
I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!
He's lean he's keen... He's the spank machine!
Eh, no! Only call srand(time(NULL)) once in your program. Not every time you retrieve a random number.Originally posted by devour89
Code:int random(int low, int high) { srand(time(NULL)); return low + rand()%(high-low+1); }
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
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: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.
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
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.