Computing.Net > Forums > Programming > random choice with prob in java

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

random choice with prob in java

Reply to Message Icon

Name: karim
Date: May 7, 2005 at 12:41:51 Pacific
OS: xp
CPU/Ram: 1.8
Comment:

Hi guys,
Anybody knows how to randomly choose one of two numbers but with specifying a probability. For exemple, each time randomly choose one of two colored balls, knowing that you have a 75% probality to choose the red ball. something lke that...any ideas?
I would really apreciate the help
thank you in advance



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: May 7, 2005 at 12:49:03 Pacific
Reply:

I think you need to get clear about some concepts.

If it's truly random, which ain't easy to implement, the chance of either is 50%.

M2


If at first you don't succeed, you're about average.


0

Response Number 2
Name: Wolfbone
Date: May 7, 2005 at 14:28:11 Pacific
Reply:

Math.random() returns a double from a uniform distribution in [0.0,1.0) does it not? How often will it return a number less than 0.75?


0

Response Number 3
Name: wizard-fred
Date: May 7, 2005 at 17:00:01 Pacific
Reply:

You can load the probability by choosing a selection value of greater than .25 instead of .5 for a 75% chance rather than a 50% chance.


0

Response Number 4
Name: karim
Date: May 7, 2005 at 23:20:19 Pacific
Reply:

Thank you guys a bunch. I couldnt figure it out even if its too simple. No need then to have a special function to do that for you in Java.
something like this I guess should work:

Random rand = new Random();
int choice = (rand.nextInt(100)<75) ? 1 : 0;

it should pick the number 1 with a probability of 75% :-)
Gotta get back to my code now!
Thanks for the tip guys
Sane


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: random choice with prob in java

Working with RS232 in Java www.computing.net/answers/programming/working-with-rs232-in-java/9983.html

Random Gaussian Vectors in Java www.computing.net/answers/programming/random-gaussian-vectors-in-java/13064.html

Help with problem in JAVA... www.computing.net/answers/programming/help-with-problem-in-java/5117.html