Thread: How to call a particular random number picker?

  1. #1
    The larch
    Join Date
    May 2006
    Posts
    3,573

    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 )
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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.
    If you understand what you're doing, you're not learning anything.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Thanks again. I called it WeightedRandoms and it seems to be good enough.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need a random number generator thats not compleatly random
    By thedodgeruk in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2011, 06:48 AM
  2. Replies: 5
    Last Post: 10-05-2009, 10:21 AM
  3. Replies: 2
    Last Post: 12-25-2003, 01:31 AM
  4. random number between negative and positive number
    By anomaly in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2003, 08:40 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM