For our class the teacher wants us to use a user input seed for the srand function,, but every time it is called she wants it to have a different random number output. I keep getting the same random number (we have to use a user input seed, cannot change to using time as the seed). How is it possible to do this and get a different random numbers each time?
int main() {
cin >> seed;
srand(seed);
}
//my function is from a game of chutes and ladders
void playGame (int players[], int numPlayers, int gameboard[], int boardSize, int maxSpin)
{
bool winner = false;
do {
int n;
for (int i = 0; i < numPlayers; i++) {
n = players[i] + (rand() % (maxSpin - 1 + 1)) +1;
if (n > boardSize){
players[i] = gameboard[n];
}
if (players[i] = boardSize){
winner = (true);
}
}
}while (winner = false);
}
I'm trying to get it to output a different number each time with the same seed.
This is just like saying you dont want to use srand()
1 2 3
int a;
a = rand()%10;
cout<<a;
1 2 3
srand(3);
int a = rand()%10;
cout<<a;
There is nothing different with these 2 programs except for the fact that on their first run, they may produce different numbers but will remain the same for ever.
Running the program with the same seed every time is like the second code.
if there is any non-constant variable you can use that as a seed.
otherwise, use big series like "8712202953252362646.......42965" get next digit each time, its not perceivable to the player (unless he is a tester). for this you have to save data in memory - which digit you last used.
The point of pseudo-random number generators is that resulting value is not really random (as computer lack imagination and cannot get truly random number), but calculated from previous result. Random seed sets 0th result allowing first to be calculated.
If you input same seed, you will get same sequence of numbers. This is a trait of PRNG. A pretty useful trait: you can save only seed and restore results later.
In Minecraft you can enter same seed and get same world. In Dune 2 campain maps landscape is stored as single number: seed.