Reputation:
What I need this program to do is roll 36000 2d6, output the results of each value and how often it occurs in a table format. Unfortunately I'm unfamiliar with how arrays work. This is what I have so far:
int DiceArray()
{
int rollOne = 0;
int rollTwo = 0;
int countrolls = 0;
int sum = 0;
for (countrolls=1; countrolls<=36000; countrolls++)
{
rollOne = (rand() % 6) + 1;
rollTwo = (rand() % 6) + 1;
sum = rollOne+rollTwo;
}
}
So, I need an array for the dice results which I'm guessing is gonna look like result[11] because it lists 2 through 12 for the sum of the dice. Then I'm gonna have to make the array multidimensional; I'll need a second column for the results.
So, for instance, the result of two would occur we'll say 700 times. So I'd need something like outcome[2]. Is that right? And how would I get the right values for my array, anyways?
I suppose for the result array I just list them like so since they'll always be the same: {2, 3, 4,... 12}
But how do I output my sum to array?
Upvotes: 1
Views: 4967
Reputation: 275385
This is a C++11 answer. Based off this stack overflow answer
typedef std::mt19937 MyRNG; // the Mersenne Twister with a popular choice of parameters
std::vector< unsigned > DiceArray(
unsigned how_many_rolls, unsigned dice_count,
MyRNG& rng
)
{
// d6!
std::uniform_int_distribution<uint32_t> d6(1,6);
std::vector< unsigned > retval;
retval.resize( dice_count * 6+1 );
for (unsigned count = 0; count < how_many_rolls; ++count)
{
unsigned sum = 0;
for(unsigned i = 0; i < dice_count; ++i) {
sum += d6(rng);
}
retval[sum] += 1;
}
return retval;
}
And next we use it:
int main(int argc, char* argv[])
{
MyRNG rng;
uint32_t seed_val = 0; // populate somehow -- as `0` it will replicate the same sequence each time. A common trick is to grab the current time or some other source of entropy
rng.seed(seed_val); // avoid calling this more than once per experiment. It resets the RNG, if you call it again it will repeat the same sequence of numbers as the first time.
std::vector<unsigned> result = DiceArray(36000, 2, rng);
for (unsigned i = 0; i < result.size(); ++i) {
std::cout << i <<": " << result[i] << "\n";
}
}
Upvotes: 0
Reputation: 60381
Something like this should work:
#include <iostream>
#include <random>
#include <array>
std::array<std::size_t, 13> DiceArray(const std::size_t count)
{
std::random_device device;
std::mt19937 engine(device());
std::uniform_int_distribution<std::size_t> distribution(1, 6);
std::array<std::size_t, 13> result = {};
for (std::size_t i = 0; i < count; ++i) {
++result[distribution(engine)+distribution(engine)];
}
return result;
}
int main(int argc, char* argv[])
{
auto result = DiceArray(36000);
for (std::size_t i = 0; i < result.size(); ++i) {
std::cout<<i<<" "<<result[i]<<std::endl;
}
return 0;
}
Upvotes: 1
Reputation: 62073
Your idea of result[11];
would work. You would have to zero-initialize it too.
int result[11] = {0};
Keep in mind that arrays are zero-based. So this array would cover the range of 0-10. You can work with that by subtracting off the minimum dice roll. Increment the corresponding array location for each roll in your loop:
++result[sum-2];
Accessing the value again requires subtracting the minimum dice roll:
int numTwos = result[2-2];
int numTens = result[10-2];
Upvotes: 0
Reputation: 21749
Not sure, what you're asking, but it seems like you need a simple histogram. Like this:
void DiceArray()
{
int rollOne = 0;
int rollTwo = 0;
int sum = 0;
// This array holds histogram. hist[0] and hist[1] are always zero.
int hist[13] = { 0 };
for (int countrolls = 0; countrolls < 36000; ++countrolls)
{
rollOne = (rand() % 6) + 1;
rollTwo = (rand() % 6) + 1;
sum = rollOne+rollTwo;
hist[sum]++;
}
for (int i = 2; i <= 12; ++i)
{
std::cout << i << ": " << hist[i] << std::endl;
}
}
This function prints the following:
2: 949
3: 1974
4: 2898
5: 3987
6: 5133
7: 6088
8: 4944
9: 3976
10: 3075
11: 1991
12: 985
Upvotes: 3