Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Implementing probability in Java

1. ## 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.....

2. ## 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:
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,
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%.

3. ## The Following User Says Thank You to GregBrannon For This Useful Post:

PushBilly (June 7th, 2014)

4. ## Re: Implementing probability in Java

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!