loops and more loops plus a little probability

I have trying to write this code for days now with little success.
I am trying to create a code that will simulate a roll of the dice.
The hold number is the minimum number that needs to be reached and the probability has to be displayed after each count.

Please any help is appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  #include <iostream>
#include <cstdlib>
#include <ctime>

using std::cout;
using std::cin;
using std::endl;

int dieRoll();
int humanTurn(int);
int hold, roll;

int main()

{
	srand(time(0));
	
	cout<<"Enter the amount to hold"<<endl;
	cin>>hold;
	cout<<""<<endl;
	cout<<"enter how many times to roll"<<endl;
	cin>>roll;
	cout<<""<<endl;
	
	const int MIN_NUMBER=hold, MAX_NUMBER=hold+6;
	
	int num;
	
	cout<<"Score"<<"\t"<<"Estimated Probability"<<endl;
	
	for (num=MIN_NUMBER; num <= MAX_NUMBER;num=(rand() % 6))
	cout<<num<<"\t\t"<<"probability"<<endl;
	return 0;
}


Also here are some examples of the code output when it is working properly.

Example Runs (User input has been bolded and underlined for emphasis.)



What value should we hold at? 17

Hold-at-N turn simulations? 10000000

Score Estimated Probability

0 0.570301

17 0.114186

18 0.108193

19 0.083909

20 0.062619

21 0.040803

22 0.019988


What value should we hold at? 21

Hold-at-N turn simulations? 2

Score Estimated Probability

0 0.000000

21 0.500000

22 0.000000

23 0.500000

24 0.000000

25 0.000000

26 0.000000



What value should we hold at? 21

Hold-at-N turn simulations? 4

Score Estimated Probability

0 0.250000

21 0.250000

22 0.250000

23 0.250000

24 0.000000

25 0.000000

26 0.000000

Last edited on
I don't understand that ._.a here is my way to do it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 int dice[6], roll;
double prob[6];
cin>>roll;
for(int i=0;i<roll;i++){
   int x=rand()%6;
   for(int j=1;j<=6;j++)
      if(j==x){ 
         dice[j-1]+=1;
         break;
      }
}
for(int i=0;i<6;i++){
   prob[i]=dice[i]/6
}


Idk if my code is really find probability xD
1) rand() % 6 is skewed and not uniformly distributed. Use std::uniform_distribution

2) In each simulation you need to roll dice until sum reaches hold number.
3) As soon as you stop you need to increase respective value in array which holds amount of simulations stopped at particular number.
4) To get probability divide amount for particulat number by total amount of simulations
5) What with 0 score?
closed account (48T7M4Gy)
Looks like 0 value refers to the residual probability.
I am just working on getting the prog to produce the 0 residual value and 6 numbers after first then I will work on getting probability.
My code does not display any numbers, why is that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <cstdlib>
#include <ctime>

using std::cout;
using std::cin;
using std::endl;

int dieRoll();
int humanTurn(int);
int hold, roll;

int main()

{
	srand(time(0));
	
	cout<<"Enter the amount to hold"<<endl;
	cin>>hold;
	cout<<""<<endl;
	cout<<"enter how many times to roll"<<endl;
	cin>>roll;
	cout<<""<<endl;
	
	int dice;
	
	int num;
	dice=(rand() % 6 + 1);
	
	cout<<"Score"<<"\t"<<"Estimated Probability"<<endl;
	
	for (num=0; num>=hold;num=(num+dice))
	cout<<num<<"\t\t"<<"probability?"<<endl;
	return 0;
I finally understand what I need to do

let's suppose hold at number is 17

We role the dice until we roll a 17 or larger, but if we roll a 1, then our score is 0.

Let's suppose the number of simulations is 5 and for simulation 1 we roll

2 + 5 + 6 + 4 = 17

and for simulation 2 we roll

5 + 4 + 1, but since we rolled a 1, the score is 0,

for simulation 3 we roll

3 + 4 + 5 + 6 = 18

for simulation 4 we roll

4 + 4 + 6 + 6 = 20

for simulation 5 we roll

3 + 4 + 5 + 5 = 17 so that means we got

0: 1 time 1/5 = .20
17: 2 times 2/5 = .40
18: 1 time 1/5 = .20
19: 0 times 0/5 = .00
20: 1 time 1/5 = .20
21: 0 times 0/5 = .00
22: 0 times 0/5 = .00

In other words the idea is to roll a 17 or larger (or whatever the hold number happens to be), but if you roll a 1, then your score is a 0.

Anyone have any ideas or tips on how to accomplish this?
Last edited on
Topic archived. No new replies allowed.