Help with Probabilities & C++ Programming

  • Context: C/C++ 
  • Thread starter Thread starter Tenshou
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around assigning probabilities to variables in C++ programming, particularly in the context of using conditional statements and loops. Participants explore how to implement probabilistic behavior in code, including generating random numbers and structuring loops.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant expresses uncertainty about how to assign probabilities to variables in C++.
  • Another participant asks for clarification on whether the original poster understands how to assign probabilities on paper, suggesting that it should translate easily to code.
  • A participant describes a method for using random numbers to create conditional statements that mimic probabilities, providing a code example using the C standard library.
  • There is a question about how to create a loop that continues running without terminating.
  • Another participant suggests that a loop condition can be constructed to never become false, implying a way to create an infinite loop.
  • One participant clarifies that the previous code example was not a loop but an if-else block, suggesting that it should be embedded in a loop.
  • A participant proposes using a boolean variable set to true to maintain the loop, while another suggests using a while(true) structure instead.
  • Finally, a participant offers an alternative approach to generating probabilities within a loop, using a random number divided by RAND_MAX to determine actions based on defined probability thresholds.

Areas of Agreement / Disagreement

Participants generally agree on the approach of using random numbers to simulate probabilities in C++, but there are varying suggestions on how to implement loops and manage conditions. The discussion remains unresolved regarding the best practices for maintaining loops and assigning probabilities.

Contextual Notes

Some participants mention the limitations of the rand() function as a pseudo-random number generator, but do not reach a consensus on its adequacy for the task at hand. There is also a lack of clarity on the specifics of loop termination conditions.

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?
 
Technology 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!
 
  • #10
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
   }
}
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
86
Views
3K
Replies
69
Views
11K
Replies
81
Views
8K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 25 ·
Replies
25
Views
2K
  • · Replies 15 ·
Replies
15
Views
8K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 54 ·
2
Replies
54
Views
5K