Help with Probabilities & C++ Programming

  • C/C++
  • Thread starter Tenshou
  • Start date
  • Tags
    C++
In summary, In order to assign probabilities to variables in C++, you would need to use the rand() function to get a random number, and then divide that number by RAND_MAX to get a probability.
  • #1
Tenshou
153
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
  • #2
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.
 
  • #3
No, I mean how do you give a variable a probability, for example, in a conditional statement you will printf(hello world) when the probablity 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?
 
  • #4
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.
 
  • #5
Thank you! How would you make this loop keep going without terminating?
 
  • #6
Do you know how to make a loop that does terminate? Simply use a loop condition that never becomes 'false'.
 
  • #7
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.
 
  • #8
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?
 
  • #9
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
   }
}
 

1. What is probability and why is it important in programming?

Probability is the measure of how likely an event is to occur. It is important in programming because it allows us to make informed decisions and predictions based on data and statistics. It is used in various fields such as machine learning, data analysis, and game development.

2. How can I calculate probabilities in C++?

C++ has built-in functions and libraries that allow you to perform mathematical calculations, including probabilities. You can use the rand() function to generate random numbers and the math.h library for more complex calculations.

3. Can you provide an example of using probabilities in a C++ program?

Sure! Let's say you want to create a simple game where the player has to guess a number between 1 and 10. You can use the rand() function to generate a random number and then ask the player to guess. If their guess is correct, they win. The probability of winning in this game would be 1/10 or 10%.

4. How can I improve my understanding of probabilities in C++ programming?

Practice and experimentation are key to improving your understanding of probabilities in C++. You can also read books or online resources on probability theory and its application in programming. Additionally, working on projects that involve probability calculations can help you gain practical experience.

5. Are there any common mistakes to avoid when working with probabilities in C++?

One common mistake is not properly understanding the concept of probability and how to calculate it. Another mistake is using the wrong functions or libraries for your specific needs. It is important to carefully read the documentation and make sure you are using the correct methods and formulas for your particular problem.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
728
  • Programming and Computer Science
2
Replies
54
Views
3K
  • Programming and Computer Science
Replies
7
Views
468
  • Programming and Computer Science
Replies
8
Views
877
  • Programming and Computer Science
2
Replies
69
Views
4K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
3
Replies
81
Views
5K
Back
Top