The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
February 2002

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

just a suggestion

Posted by ping203 on February 22, 2002 at 6:24 PM

Hi,

I am not too good at Math, but here is my suggestion.

Using the way you generate random, 1-10, total 10 possibilities.
Let say you want the probablity accurate to 0.01,
0 - 0.1
1 - 0.2
2 - 0.21
3 - 0.09
4 - 0.1
5 - 0.1
6 - 0.2
7 - 0.06
8 - 0.01
9 - 0.01
10 - 0.01
total - 1.00
Then, you create an array, size 100.
Then, you put 10 1's, 20 2's, 21 3's, 10 4's,...,1 10 in this array.
Then, instead generating 1-10, you generate 0-99(array index). The probability of each random number is the same(assume Java does a good job)
Then, use this random number to index the array.
The result will be you get 1 in 1 out of 10, 2 in 2 out of 10,..., 10 in 1 out of 100.

(the way you store 1-10 in the array does matter, as long as the number of each NUMBER is the same as the probability you want it to be drawn.)

hope you can understand me.

good luck
Ping



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us