I was wondering what the way is to get a random number with a range of 1-6, using the rand() method. This is to simulate a dice roll needed for me to find the average of 3 dice rolls so the type will be double.
Asked
Active
Viewed 3.3k times
7
-
Possible duplicate of [How to generate a random number in C++?](https://stackoverflow.com/questions/13445688/how-to-generate-a-random-number-in-c) – Apr 21 '19 at 17:55
-
I think this question should be closed, read this: https://stackoverflow.com/a/13445752/5550963 – Apr 21 '19 at 17:55
1 Answers
12
This is a simple example to generate randoms between 1 to 6, I think you can figure the rest
#include <iostream>
#include <cstdlib>
#include <ctime>
int main() {
srand(time(0));
std::cout << (rand() % 6 + 1) <<std::endl;
return 0;
}

Nylon Smile
- 8,990
- 1
- 25
- 34
-
The terribleness of `rand()` aside, this is almost certainly going to be biased. – T.C. Feb 07 '15 at 07:20