Help with Probabilities & C++ Programming

  • Context: C/C++ 
  • Thread starter Thread starter Tenshou
  • Start date Start date
  • Tags Tags
    C++
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
9 replies · 3K views
Tenshou
Messages
153
Reaction score
1
I am not sure how to assign probabilities to variables do you think you guys will be able to help me?
 
Physics news on Phys.org
Can you be more specific? Do you know how to assign probabilities on paper? If so, it shouldn't be too hard to translate your paperwork into source code.
 
No, I mean how do you give a variable a probability, for example, in a conditional statement you will printf(hello world) when the probability is 1/3 or you will printf(Good Day!) if the probability is 1/3 or printf(Good Bye) if the probability is 1/3. What I mean is how will I get a conditional statement to run as if they were probablities... would I just conjoin it with if, else ,when statements?
 
I don't use C++, but you can generate a random number and then decide on a course of action depending on the result. For example:

Code:
#include <stdlib.h> // C standard library
float num = rand() % 9; // this is the C rand function, and your number will be between 0-8

if (num < 3)
{
  // do something
}
else if (num >= 3 && num <= 6)
{
  // do something else
}
else // implication num > 6 && num <= 8
{
  // do something else
}

Your random number will be in one of the 3 ranges with 1/3 probability, although it's worth noting that rand() is a pseudo-random number generator, but it's probably fine for your purposes.
 
Thank you! How would you make this loop keep going without terminating?
 
Do you know how to make a loop that does terminate? Simply use a loop condition that never becomes 'false'.
 
Adyssa said:
I don't use C++, but you can generate a random number and then decide on a course of action depending on the result. For example:

Code:
#include <stdlib.h> // C standard library
float num = rand() % 9; // this is the C rand function, and your number will be between 0-8

if (num < 3)
{
  // do something
}
else if (num >= 3 && num <= 6)
{
  // do something else
}
else // implication num > 6 && num <= 8
{
  // do something else
}

Your random number will be in one of the 3 ranges with 1/3 probability, although it's worth noting that rand() is a pseudo-random number generator, but it's probably fine for your purposes.

Tenshou said:
Thank you! How would you make this loop keep going without terminating?
What Adyssa wrote is NOT a loop; it's an if...else block. To make this a loop, you would need to embed the if-else block in a loop of some kind.
 
when I said "when statement" I meant to say while sorry >.< so I would make a bool variable and have it always true while the conditional is looping or something of that fashion?
 
Tenshou said:
so I would make a bool variable and have it always true while the conditional is looping or something of that fashion?

Correct, but you don't need to use a variable. This is usually written as
Code:
while (true) {
// do something
}

The "do something" code probably needs to contain a "break" statement somewhere, unless you really want the program to run for ever!
 
You could do something like this:
Code:
while (1)
{
   Get a random number (in the range 0 .. RAND_MAX)
   Turn it into a probability by dividing by RAND_MAX
   if (prob < .3333)
   {
      do something
   }
   else if (prob < .6666)
   {
      do something else
   }
   else 
   {
      do the third thing
   }
}