# Implementing probability in Java

• June 7th, 2014, 09:43 AM
PushBilly
Implementing probability in Java
You're given a number between 0 and 1 that corresponds to the probability of an event occurring.
0.7 is 70%, 0.235 is 23.5%, 0 is 0%, etc.

Someone tries to perform the event, but the event only has the given probability of occurring. Run the probability, and then if it's true, run the event. Otherwise don't run the event.

How do you do that?

All I know how to do is generate random numbers (kind of), but if you have 0.2245 I really don't see how generating random numbers can efficiently perform that probability.....
• June 7th, 2014, 12:05 PM
GregBrannon
Re: Implementing probability in Java
Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

I don't think you've stated the problem well. This statement is particularly confusing:
Quote:

Someone tries to perform the event, but the event only has the given probability of occurring. Run the probability, and then if it's true, run the event. Otherwise don't run the event.
Random numbers or booleans can be used to make logical branching decisions. For example, if a generated random number is greater than a certain amount, then perform this action, otherwise do something else, possibly nothing. That kind of logic can be used to simulate behaviors or results defined by their probability of occurring. You might have heard the term "Monte Carlo Simulation" which is often used to randomly decide outcomes of events. You can search for that term and learn more about it, but that's probably more than you need.

If you can state the problem better, perhaps using an example of what you need to do or by stating the actual problem exactly as it was given to you, we might be more helpful.

Edit: Focusing on your last statement,
Quote:

if you have 0.2245 I really don't see how generating random numbers can efficiently perform that probability
The chance or probability that a uniformly distributed randomly generated number between 0 and 1 is less than or equal to 0.2245 is 22.45%.
• June 7th, 2014, 03:01 PM
PushBilly
Re: Implementing probability in Java
Quote:

Originally Posted by GregBrannon
Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

Edit: Focusing on your last statement,

The chance or probability that a uniformly distributed randomly generated number between 0 and 1 is less than or equal to 0.2245 is 22.45%.

That does it right there! Thanks!