# Random Numbers

To roll dice we need to generate a random number, then find the remainder when we divide by 6. This will give us a value from 0 to 5. We add 1 to that to get a value from 1 to 6.

1 2 3 4 5 6 7 8 9 10 11 12 13
| //Random numbers
#include<iostream>
#include <ctime> //for the time to set the random number seed
using namespace std;
int main() {
int die1, die2;
srand((unsigned)time(0)); //to set random number seed
die1 = rand() % 6 + 1; //a random number between 1 and 6
die2 = rand() % 6 + 1; //a random number between 1 and 6
cout <<"You rolled "<<die1<<" and "<<die2<< endl;
system("pause");
return 0;
}//main |

CODERun this program a couple of times to make sure you get new values each time.