Using % with rand is wrong.
Assuming your RAND_MAX is the same as mine 2147483647
then the probabilities for each number are:
dice1 = rand() % 6 + 1;
1: 357913942/2147483647 Notice a slightly higher probability for a 1.
2: 357913941/2147483647
3: 357913941/2147483647
4: 357913941/2147483647
5: 357913941/2147483647
6: 357913941/2147483647
The solution use C++11 random C++11 random functionality.
Correct for the skew in C++03's rand()
Unfortunately I can't find a correct answer on SO for using rand().
int dieRoll() // return a number between 1 and 6
{
static int maxRange = RAND_MAX / 6 * 6; // note static so calculated once.
int result;
do
{
result = rand();
}
while(result > maxRange); // Anything outside the range will skew the result
return result % 6 + 1; // So throw away the answer and try again.
}
Note:
int result = rand() * 1.0 / range; // does not help with distribution
Using % with rand is wrong.
Assuming your RAND_MAX is the same as mine 2147483647
then the probabilities for each number are:
dice1 = rand() % 6 + 1;
1: 357913942/2147483647 Notice a slightly higher probability for a 1.
2: 357913941/2147483647
3: 357913941/2147483647
4: 357913941/2147483647
5: 357913941/2147483647
6: 357913941/2147483647
The solution use C++11 random functionality.
Correct for the skew in C++03's rand()
Unfortunately I can't find a correct answer on SO for using rand().
int dieRoll() // return a number between 1 and 6
{
static int maxRange = RAND_MAX / 6 * 6; // note static so calculated once.
int result;
do
{
result = rand();
}
while(result > maxRange); // Anything outside the range will skew the result
return result % 6 + 1; // So throw away the answer and try again.
}
Note:
int result = rand() * 1.0 / range; // does not help with distribution
Using % with rand is wrong.
Assuming your RAND_MAX is the same as mine 2147483647
then the probabilities for each number are:
dice1 = rand() % 6 + 1;
1: 357913942/2147483647 Notice a slightly higher probability for a 1.
2: 357913941/2147483647
3: 357913941/2147483647
4: 357913941/2147483647
5: 357913941/2147483647
6: 357913941/2147483647
The solution use C++11 random functionality.
Correct for the skew in C++03's rand()
Unfortunately I can't find a correct answer on SO for using rand().
int dieRoll() // return a number between 1 and 6
{
static int maxRange = RAND_MAX / 6 * 6; // note static so calculated once.
int result;
do
{
result = rand();
}
while(result > maxRange); // Anything outside the range will skew the result
return result % 6 + 1; // So throw away the answer and try again.
}
Note:
int result = rand() * 1.0 / range; // does not help with distribution
Using % with rand is wrong.
Assuming your RAND_MAX is the same as mine 2147483647
then the probabilities for each number are:
dice1 = rand() % 6 + 1;
1: 357913942/2147483647 Notice a slightly higher probability for a 1.
2: 357913941/2147483647
3: 357913941/2147483647
4: 357913941/2147483647
5: 357913941/2147483647
6: 357913941/2147483647
The solution use C++11 random functionality.
Correct for the skew in C++03's rand()
Unfortunately I can't find a correct answer on SO for using rand().
int dieRoll() // return a number between 1 and 6
{
static int maxRange = RAND_MAX / 6 * 6; // note static so calculated once.
int result;
do
{
result = rand();
}
while(result > maxRange); // Anything outside the range will skew the result
return result % 6 + 1; // So throw away the answer and try again.
}
Note:
int result = rand() * 1.0 / range; // does not help with distribution