Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Probabilty Problem

Started by Nomad010 Nov 12, 2004 at 11:38 AM 9 replies 900+ views
Original Post
Nomad010
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?
"..."
nagromo
nagromo
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.
Rebooted
Rebooted
Call Java's random function, and if the return value is 1 million, set your bool to true. Otherwise, set it to false.
Nomad010
Nomad010
Java's Math.random function only produces a random number from 0.000 to 1.000. There are only 1001 possibilities.
"..."
Thex0
Thex0
That's a floating point number, so if((int)(Math.random()*1000000.0) == 1000000)
tombr
tombr
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.
pinacolada
pinacolada
Don't use Math.random, use java.util.Random, and call nextInt(). The code looks like this:

//create the random, note how we seed itRandom r = new Random(System.currentTimeMillis());// ....boolean bOneInAMillion = (r.nextInt() % 1000000) == 0;


ps. floating point numbers do indeed have way more than 1001 possible values
Nomad010
Nomad010
Oops. I was confusing the Math.random() function for another class. *cough*.

Ratings++ for tombr and Thex0.
"..."
Nomad010
Nomad010
Yeah. We all have 1000 ratings so it doesn't really do anything.
"..."
Thygrrr
Thygrrr
Are any of these two available in MIDP 1.0/2.0 ?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.