Hey, Im new to these forums and this is my first post, so please dont be too mean =P. Anyways, Im tryin to Learn C++, and im messin around with rand() and functions just so I can get a feel for how they work. So, I wrote this program that takes in 2 numbers, the first is how many 6-sided dice you want it to roll, and the second is how many times you would like to simulate the rolling. It then counts the number of times that 1,2,3,4,5,6 comes up and totals them up. It works pretty good except for 1 thing, i never get any sixes, I looked at the code for a while and can't figure out why its doing that. So if someone could please take the time to find out where I went wrong I would really appreciate it =). THanks alot!
Code:
#include <iostream>
#include <math.h>
#include <time.h>
#include <stdlib.h>
using namespace std;
void random(int n);
int num_of_dice=1,num_of_times=1;
int main()
{
srand(time(NULL));
while(num_of_dice)
{
cout << "How many dice should we roll?";
cin >> num_of_dice;
cout << endl;
cout << "How many time should we roll " << num_of_dice << " dice?";
cin >> num_of_times;
cout << endl;
random(num_of_dice);
}
}
void random(int n)
{
int k1=0,k2=0,k3=0,k4=0,k5=0,k6=0;
int kT=0;
for(int q=1;q<=num_of_times;q++)
{
for(int i=1;i<=num_of_dice;i++)
{
int r=(rand()%5)+1;
switch(r)
{
case 1:
k1++;
break;
case 2:
k2++;
break;
case 3:
k3++;
break;
case 4:
k4++;
break;
case 5:
k5++;
break;
case 6:
k6++;
break;
default:
cout << "Error" << endl;
break;
}
}
kT=k1+k2+k3+k4+k5+k6;
cout << "One : " << k1 << endl << "Two : " << k2 << endl << "Three : ";
cout << k3 << endl << "Four : " << k4 << endl << "Five : " << k5 << endl;
cout << "Six : " << k6 << endl << "Total : " << kT << endl << endl;
}
}
I attached the cpp file too. I think