# How to call a particular random number picker?

• 09-06-2011
anon
How to call a particular random number picker?
Not being a native speaker, how would you call a class that returns one of given values with a given probability?

The usage might be like this:
Code:

```.... random_number_picker({'a':10, 'b':8, 'c':4, 'd':6}); random_number_picker.get(); //returns 'a' with probability 10/28, 'b' - 8/28 etc```
(If I knew how it might be called, may-be I could find an existing one on the Internet :))
• 09-06-2011
itsme86
The "probabilities" you've attached are referred to as "weights". If you do a google search for "weighted random values algorithm" you should get plenty of websites that will give you what you're looking for.

One way is to basically create an array of the values "weight" times. So, with your example, you'd end up with an array with 10 'a's followed by 8 'b's, etc: aaaaaaaaaabbbbbbbbccccdddddd. From there you can simply pick a random value from that array.
• 09-06-2011
anon
Thanks.

My idea was to build an array of key-value pairs from the dictionary, where the key is the upper bound for selecting this value. Then get draws a random number and iterates this array until the probability key becomes higher than the random number. (I know in C++, I could use upper_bound, but this is Actionscript and the number of different outcomes is going to be small anyway.)
• 09-06-2011
tabstop
The fancy word is "distribution". You'll find a lot of pre-defined distributions and not so many that give you a way to define your own. Your algorithm is the basic idea. You can get fancy (you can always get fancy) but I doubt you'll need it.
• 09-06-2011
anon
Thanks again. I called it WeightedRandoms and it seems to be good enough.