Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

probability c++ question

Tags:

c++

I have to make a game of craps and towards the end, I have to do some probability. Here is my code so far. I want it so that the loop repeats 1000 times and looks for the 'probNumb' that the user entered. I am not sure if did this right but lets say I entered the number 5. This is what I get.
"Out of 1000 times, 5 was rolled 1000 times."

So, its not counting how many times 5 was rolled. I am not allowed to use break or continue statements, only loops and if else.


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;
like image 762
Naman Avatar asked Dec 02 '22 01:12

Naman


2 Answers

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

It's 1000 because the assignment returns the value assigned and since that's always non-zero in this case, it's always true.

like image 124
Jeff Foster Avatar answered Dec 20 '22 15:12

Jeff Foster


it's a typo

if (ranNumbFin = probNumb)

should be

if (ranNumbFin == probNumb)
like image 28
Karoly Horvath Avatar answered Dec 20 '22 15:12

Karoly Horvath