I have written this code which will simulate the roll of 2 dice. When I run it with just the 2 dice rolling it seems to give me a random number between 2 and 12 (which is what I want). However when I try to simulate the roll multiple times (using a for loop) it always bins the data in the same vector space. If I run it for an extremely large number of times it does start to separate out a little bit. This points me towards thinking that it is the random seed that is the issue, however I'm not sure how to correct this.
//using rand to generate a uniformly distributed number between 0 and 1. Multiplying that number by 6, adding 1 to it and then rounding it down to simulate a dice throw. Rolling 2 dice together to get a total between 2 and 12, then keeping a tally of rolling 2 dice a given number of times.
#include <iostream>
#include <vector>
#include <iostream>
#include <stdlib.h>
#include <cstdlib>
#include <time.h>
using namespace std;
double rnd ()
{
//defining the variables as doubles
double n;
double y;
//setting up the random number seed
srand (time(NULL));
//using the rand command to generate a pseudo-random number between 0 and 1
n = ((double)rand()/(double)RAND_MAX);
return n;
}
int dice ()
{
double x = rnd()*6;
int y = x+1;
return y;
}
int two_dice ()
{
int throw_1 = dice();
int throw_2 = dice();
int score = throw_1 + throw_2;
return score;
}
int main ()
{
//asking the user for the number of trials
int n_trials;
cout << "plese enter the number of trial that you wish to simulate \n";
cin >> n_trials;
//creating the vector to store the data in
vector<int> dice_throws(11); //has 12 bins to store data in
int sum_dice;
//using a for loop to roll the dice multiple times
for (int roll = 0; roll <= n_trials; roll++) {
sum_dice = two_dice();
dice_throws[sum_dice - 2]++;
}
for (int y = 0; y<dice_throws.size()+1; ++y) {
cout << dice_throws[y] << "\n";
}
return 0;
}