Naman
Naman

Reputation: 247

probability c++ question

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 << "What number do you want the probability of ?";
cin >> probNumb;

while (probCount < 1000)
{
  ranNumb= 1 + (rand() % (5 + 1));
  ranNumb2= 1 + (rand() % (5 + 1));
  ranNumbFin = ranNumb + ranNumb2;
  probCount++;

  if (ranNumbFin = probNumb)
    probNumbCount++;
}

cout << "Out of 1000 times, " << probNumb << " was rolled " 
  << probNumbCount << "times." << endl;

Upvotes: 4

Views: 494

Answers (4)

Jason B
Jason B

Reputation: 1

My use of C and C++ is rusty, but I believe the ranNumb and ranNumb2 are not going to behave like dice rolls. These will just give a uniform random variate over 0 to 1.

Conceptually, for a six sided dice:

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

An so on. There is probably a better performing method.

Upvotes: 0

g.d.d.c
g.d.d.c

Reputation: 47988

Your line if (ranNumbFin = probNumb) should be if (ranNumbFin == probNumb) - you're assigning, not comparing, which is causing the probNumbCount to increment every time.

Upvotes: 4

Karoly Horvath
Karoly Horvath

Reputation: 96258

it's a typo

if (ranNumbFin = probNumb)

should be

if (ranNumbFin == probNumb)

Upvotes: 4

Jeff Foster
Jeff Foster

Reputation: 44706

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.

Upvotes: 10

Related Questions