Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Probability of events in java

  1. Jul 13, 2012 #1
    Hello.

    So I'm wondering if there is a way to assign a probability to a certain event in the code.

    Let's say that I want a certain arithmetic operation to have an 80% chance of happening (and make an if/else if statement based on that, or something similar...)

    Is there something in the java language that can help me? or should I just create an algorithm to do it?

    Thanks a lot.
     
  2. jcsd
  3. Jul 13, 2012 #2

    chiro

    User Avatar
    Science Advisor

    Hey stonecoldgen.

    Although computers are engineered to always perform an instruction and get the right output pretty much 100% of the time, what you can do is use a good random number generator to simulate the random behaviour: it won't be purely random but it should be good enough.

    So what you can actually do is have two options: the output of not doing something and the output of your arithmetic operation occuring. So in pseudo-code:

    Code (Text):

    randomnumber = generate_uniform_random_number_between_0_and_1();

    if (randomnnumber < 0.8)
       do_arithmetic_operation();
    else
       dont_do_arithmetic_operation_or_something_else();
    end if

     
    You can implement other stuff the same kind of way.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Probability of events in java
  1. Java Resources (Replies: 4)

  2. [Java] PNGException (Replies: 2)

  3. JAVA GridLayout (Replies: 1)

  4. Java interpreter (Replies: 8)

  5. Java Cylinders (Replies: 2)

Loading...