I'm trying to create a program that is designed to determine how many elements a video game character can control. In this setting, the general population can control at least 1 element. But I want 1 out of every 1000 people to have 2 elements they can control, 1 out of every 10,000 people can control 3 elements and 1 out of every 100,000 people can control 4.
The program start by asking how many are in the population. This will equal how many sides the die has. Then, it loops through, generating random numbers and then outputting how many people can control how many elements.
I run into an issue that I think had to do with my data type. Even if I put in a population of 1,000,000 the loop iterates a million times but never rolls numbers beyond 5 digits. so a population beyond 99,999 could technically never have 3 or 4 elements. It doesn't seem to go beyond 50k ish and I remember reading that one of the data types only does 65k-ish numbers. Is that the cause? What data types should I use to be able to roll random numbers between 0 and millions?
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
int main()
{
int min = 1, max, sides, roll;
double oneCounter = 0.0, twoCounter = 0.0, threeCounter = 0.0, fourCounter = 0.0;
unsigned long seed = time(0);
srand(seed);
cout << "How many characters would you like to generate?: ";
cin >> sides;
max = sides;
cout << "\n";
for (int i = 0; i < sides; i++)
{
roll = rand() % (max - min + 1) + min;
if (roll % 100000 == 0)
{
cout << roll << ": You will have 4 elements" <<endl;
fourCounter++;
}
else if (roll % 10000 == 0)
{
cout << roll << ": You will have 3 elements" <<endl;
threeCounter++;
}
else if (roll % 1000 == 0)
{
cout << roll << ": You will have 2 elements" <<endl;
twoCounter++;
}
else
{
cout << roll << ": You will have 1 element." <<endl;
oneCounter++;
}
}
cout << left;
cout << "\n==============================" <<endl;
cout <<setw(11) << "Potential"
<< setw(10) << "QTY"
<< setw(10) << "Pct"
<< endl;
cout << "==============================" <<endl;
cout << setw(11) << "Single: "
<< setw(10) << oneCounter
<< setw(10) << (oneCounter/sides)*100 <<endl;
cout << setw(11) << "Double: "
<< setw(10) << twoCounter
<< setw(10) << (twoCounter/sides)*100 <<endl;
cout << setw(11) << "Triple: "
<< setw(10) << threeCounter
<< setw(10) << (threeCounter/sides)*100 <<endl;
cout << setw(11) << "Quadruple: "
<< setw(10) << fourCounter
<< setw(10) << (fourCounter/sides)*100 <<endl;
cout << "\n\n";
return 0;
}
I have included a screen shot of the results when done for a population (sides) of 100,000 people. The probabilities looks fine, but the numbers are never more than 5 digits long.