Two dice simulator not working

Unfortunately not working. Can someone guide me in the right direction
Requirements:
(1) Create a function that simulates a throw of two dice. Hint: Create a function that simulates a throw of a die, then invoke it twice and add them.
(2) The number of throw will be one million. Count the sum of two dice. Hint: Use a loop and a one dimensional array.
(3) The result must be well formatted. The number, and histogram associated with the number of throws need to be shown. One * represents 10 thousand in the example. Hint: Use formatting and loops.

#include <iostream>
#include <iomanip>
using namespace std;

int rolldice()
{
int dice ;
dice = rand () % 6;
dice++;
return dice;
}
void OneDice()
{
int i, roll;
for (i = 1000000; i > 0; i--)
{
roll = rolldice();
OneDice();

}


}
void TwoDice()
{
int i, dice1, dice2, roll;
for (i = 1000000; i > 0; i--)
{
dice1 = rolldice();
dice2 = rolldice();
roll = dice1 + dice2;
TwoDice();
}
}

inline void star(int num)
{
int mod = num;
while (mod > 0)
{
cout << "*";
mod -= 10000;
}
}

int main()
{
int i;

srand(1000000);
TwoDice();

for (i = 1; i <= 12; i++)
{
cout << setw(2) << i << " : " << setw(6) << twodice[i] << " : ";
star(twodice[i]); cout << endl;
}

cout << endl << endl;

}
@aqmorgan12

The thing that jumps out at me, is in the OneDice and the TwoDice functions. You have for loops to count down from the one million, BUT you call the functions again in the each functions loop. The for loops will start again at one million, and get called again. Endlessly. Remove the function calls, and see if it helps.

Also, your srand() call is being seeded, so the rand numbers never change, run to run.
unfortunately @whitenite1 its still failing
OP: you need to set the seed for rand() so that it doesn't generate the same number over multiple iterations and this seed should be outside the loop. Check out the following two links:
http://stackoverflow.com/questions/13896450/rand-returns-the-same-number-each-time-the-program-is-run
http://stackoverflow.com/questions/4926622/how-to-generate-different-random-numbers-in-a-loop-in-c

Since your assignment only mentioned 'one dimensional array' I've been cheeky and used std::array rather than C-style array as the container gives you greater flexibility in using range loops as used for printing the results (does require C++11 compliant compiler). The empirical probability distribution is in line with theoretical and 1,000,000,000 (billion, not million) took 37 secs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<array>
using namespace std;
const int num_rolls = 1000000;
int main()
{
    std::array<int, 11> histo{};
    int dice1, dice2;
    srand( rand() ); // set the seed
    for (int i = 0; i < num_rolls; i++)
    {
        dice1 = rand ()%6 + 1;
        dice2 = rand()%6 + 1;
        histo[dice1 + dice2 -2]++;
    }
    for(auto& itr : histo)
    {
        cout<<itr<<"\n";
    }
}
Last edited on
Topic archived. No new replies allowed.