Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Weighted Random Number?

Ranch Hand
Posts: 296
Is there a way to do a weighted Random Number?
I need to generate a random number between 1 and 3 but I want 1 and 3 to come up most of the time and the number 2 to only come up occasionally.
For example, numbers 1 and 3 would come up 80% of the time and 2 would come up 20%.
Is there an easy way to do this that I'm not thinking of?
Thanks,
Drew

Ranch Hand
Posts: 348
Try this

some typical output :
out of 100 pick, 2 appear 23 times
out of 100 pick, 2 appear 19 times
[ November 24, 2003: Message edited by: chi Lin ]

Drew Lane
Ranch Hand
Posts: 296
This is good. Thanks for the help!
The only problem is that it's hard to change the variables easily.
For example, I might need the random numbers to be between 1-1250
and I might need to change the weight from 80/20 to 77/23, etc.
Any other thoughts?
Drew

lowercase baba
Bartender
Posts: 12602
50
will you always want a concecutive range? i.e 52-97, inclusive, but never 1,8,43,79,322, and 411
if so, the problem boils down to figuring out how to get a weighted range from 0-45, then adding 52.
=======================
if you have a set of percentages, say, 10,30,5,15,40, can you do something like:
find your "limits"... 0.1, 0.4, 0.45, 0.6, 1.00
then get a random number from 0-1, and test to see where it falls.
just a few quick thoughts, hope they might help a little...
[ November 25, 2003: Message edited by: fred rosenberger ]

Drew Lane
Ranch Hand
Posts: 296
Hmmm...
I remember looking at the standard deviation graph way back when I was in school.
Seems like I could use something like that for this. Maybe the inverse?
D.

chi Lin
Ranch Hand
Posts: 348
For weight issue, assume have two group of variables with percentage
as threshold, you can do

then use the following to decide from which group you pick element

with more if(spot ..) clause, I think this can be extended to multi groups, eg 25%Group, 40%Group, 15%Group ...
the other task will be how to fill elements into different group.
[ November 25, 2003: Message edited by: chi Lin ]

(instanceof Sidekick)
Ranch Hand
Posts: 8791
I did some weighted random routines in another language. Lemme see if I can express the ideas in Java. Say you wanted 1s and 3s to appear twice as often as 2s.

Now generalize this to something like

That's really abbreviated - you'd have to make up some data structures to hold values (1,2,3) and running total weights (40,60,80). Note the weights do not have to add up to 100 ... could have used 2,1,2 for a total of 5 just as well. Did it make any sense?
Changing weights on the fly, like adjusting 2's to now have 80, would be neat. If you're storing the total you'd have to recalculate it before looping through again. Hope all that helps.