1. ## Generating Random Numbers

Hi, I'm trying to make a program to play the game Craps. The rules of the game are working fine, but whenever I run the program, it generates the same random numbers EVERY time.

Code:
```#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

int roll();

int round = 1;
int die1;
int die2;
int total;
int point;
int rollNumber;
int numberWon = 0;
int numberLost = 0;
bool roundWin;
bool roundOver;

//BEGIN MAIN
int main() {

while(round <= 10) {
roundWin = false;
roundOver = false;
roll();

if(total == 7 || total == 11) {
roundWin = true;
roundOver = true;
cout<<"WIN!"<<endl;
}
else if(total == 2 || total == 3) {
roundOver = true;
cout<<"LOSE!"<<endl;
}
else {
point = total;
}

if(roundOver != true) {

do {
roll();
}  while (total != 7 && total != point);

if(total == point) {
roundWin = true;
roundOver = true;  \
cout<<"WIN!"<<endl;
}

else if(total == 7) {
roundOver = true;
cout<<"LOSE!"<<endl;
}
}

if(roundWin == true)
numberWon++;
else if(roundWin == false)
numberLost++;
round++;

}

cout<<"OVERALL: "<<endl;
cout<<numberWon << " wins and " << numberLost << " losses" <<endl;
system("PAUSE");

return 0;

}

int roll() {
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);

die1 = (rand() % 6) + 1;
die2 = (rand() % 6) + 1;
total = die1 + die2;
rollNumber++;

cout<<die1 << " " << die2<<endl;

}```

Some unused variables are up there, I know. I'm mainly just concerned with the roll() function because it keeps giving me the same numbers for all rolls. I tried doing the following instead --

Code:
`srand(rand());`
This gave me 10 different rolls, but everytime I ran the program it still gave me the same results. How can I create new random numbers everytime I call the roll() function?

2. You don't seed rand with rand. You seed it with something like time. Otherwise, you're seeding it with the same value every time (rand, called without having been seeded, will produce the same first result every time).

Quzah.

3. Wouldn't seconds change each time roll() is called though?

4. For a little more detail on what quzah is talking about, see here and here.

5. if u wanna know more about random number generation read this:
Mersenne Twister Random Number Generator

6. use srand() with the current time, or write a fancy random number generator class that self seeds with data from random.org (p.s. that's what I use)

7. Originally Posted by SterlingM
Wouldn't seconds change each time roll() is called though?
To answer your actual question: it might. But computers are fast these days, and a slow computer can run your function in milliseconds. So there's a good chance that the time is still the same every time you call rand.
It's unwise to seed in the function anyway. Just seed in the main function...