Probability of a C ++ Question

I have to make a craps game and in the end, I have to make some probability. Here is my code so far. I want the loop to repeat 1000 times and look for the "probNumb" that the user entered. I'm not sure if it did the right thing, but let me say that I entered number 5. This is what I get. "Of 1,000 times, 5 were rolled 1,000 times."

Thus, it was not considered how many times were carried out 5 times. I am not allowed to use break or continue statements, only loops, and if others.

cout &lt&lt "What number do you want the probability of ?"; cin >> probNumb; while (probCount &lt 1000) { ranNumb= 1 + (rand() % (5 + 1)); ranNumb2= 1 + (rand() % (5 + 1)); ranNumbFin = ranNumb + ranNumb2; probCount++; if (ranNumbFin = probNumb) probNumbCount++; } cout &lt&lt "Out of 1000 times, " &lt&lt probNumb &lt&lt " was rolled " &lt&lt probNumbCount &lt&lt "times." &lt&lt endl; 
+4
source share
4 answers

if (ranNumbFin = probNumb) is either a typo, or use ==

This is 1000 because the assignment returns the assigned value, and since in this case it is always nonzero, it is always true.

+10
source

this is a missprint

 if (ranNumbFin = probNumb) 

it should be

 if (ranNumbFin == probNumb) 
+4
source

Your line if (ranNumbFin = probNumb) should be if (ranNumbFin == probNumb) - you assign, not compare, which causes probNumbCount to increase every time.

+4
source

My use of C and C ++ is rusty, but I believe that runNumb and ranNumb2 will not behave like rolls in bone. They just give a uniform random variable from 0 to 1.

Conceptually for six-sided bone:

  u = rand(); if(u < 1/6) ranNumb=1; elseif(u < 2/6) ranNumb=2; elseif(u < 3/5) ranNumb = 3; 

So. There is probably a more efficient method.

0
source

All Articles