Sign in to follow this  
Nomad010

[java] Probabilty Problem

Recommended Posts

While doing work on a game, I came across a problem where I needed to a boolean that was true only 1 millionth of the time. Does anyone know of any Java code or classes available to allow me to do that?

Share this post


Link to post
Share on other sites
Quote:
Original post by Nomad010
While doing work on a game, I came across a problem where I needed to a boolean that was true only 1 millionth of the time.

Does anyone know of any Java code or classes available to allow me to do that?


"I needed to a boolean"

What do you mean by this? It might have just been a typo, but please fix it so we know what you mean and can help.

Share this post


Link to post
Share on other sites
Quote:
Original post by Nomad010
Java's Math.random function only produces a random number from 0.000 to 1.000. There are only 1001 possibilities.

Why do you think that. It says nothing about this in the javadoc. I don't know how many bits of precision you get, but it is defenitly more than you need.

Share this post


Link to post
Share on other sites
Don't use Math.random, use java.util.Random, and call nextInt(). The code looks like this:


//create the random, note how we seed it
Random r = new Random(System.currentTimeMillis());

// ....

boolean bOneInAMillion = (r.nextInt() % 1000000) == 0;


ps. floating point numbers do indeed have way more than 1001 possible values

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
lol, tombr and Thex0 are still at 1000, I don't think he knows what "Ratings++" means

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this