Python  1.0
Functions
weightedNumGenerator Namespace Reference

Return random numbers weighted by given probabilities. More...

Functions

def genWeightedList (l, weights)
 Method 1 – given a list of of items and a list of probabilities, return a new list that contains int(100*prob[i]) duplicates of the i-th item. More...
 
def genWeightedListD (d)
 Using a dictionary, instead of two lists. More...
 
def weightedRand (seq)
 Return a random element from seq (same of choice from random). More...
 
def weightedRand2 (l, weights)
 Method 2 – given list of items and a list of probabilities, which sum up to one, for example, 0.5 + 0.2 + 0.2 + 0.1 = 1.0. More...
 
def weightedRandD (d)
 Using a dictionary. More...
 
def main ()
 Main program for testing. More...
 

Detailed Description

Return random numbers weighted by given probabilities.

Author
Flavia e Paulo Roma
Since
16/06/2016
See also
http://programmers.stackexchange.com/questions/150616/return-random-list-item-by-its-weight

Function Documentation

◆ genWeightedList()

def weightedNumGenerator.genWeightedList (   l,
  weights 
)

Method 1 – given a list of of items and a list of probabilities, return a new list that contains int(100*prob[i]) duplicates of the i-th item.

Randomly select an item from that new list within the range [0, length(list)]

Parameters
llist of items.
weightslist of probabilities.
Returns
a list of items with multiplicity given by its corresponding probability times 100 (minimum p=0.01).

Referenced by main().

◆ genWeightedListD()

def weightedNumGenerator.genWeightedListD (   d)

Using a dictionary, instead of two lists.

Parameters
dpairs of items,probabilities.
Returns
a list of items with multiplicity given by its corresponding probability times 100 (minimum p=0.01).

Referenced by main().

◆ main()

def weightedNumGenerator.main ( )

◆ weightedRand()

def weightedNumGenerator.weightedRand (   seq)

Return a random element from seq (same of choice from random).

Parameters
seqgiven sequence of items.
Returns
random item.

Referenced by main().

◆ weightedRand2()

def weightedNumGenerator.weightedRand2 (   l,
  weights 
)

Method 2 – given list of items and a list of probabilities, which sum up to one, for example, 0.5 + 0.2 + 0.2 + 0.1 = 1.0.

Get a random number between 0 and the total weight (1.0).

  • If the random number falls in the first slot, or weight[0] (which is 0.5 in the example), then choose the first item from the list.
  • If it falls in the second slot, which is between weight[0] and weight[0] + weight[1], then select the second item from the list.
  • For third item, the random number must fall in the third slot, which would be between weight[0] + weight[1] and weight[0] + weight[1] + weight[2],
  • and the sequence goes on.

TODO - make method more efficient with quicksort

Parameters
llist of items
weightslist of probabilities.
Returns
a random item weighted by given probabilities.

Referenced by main().

◆ weightedRandD()

def weightedNumGenerator.weightedRandD (   d)

Using a dictionary.

Parameters
dpairs of items,probabilities.
Returns
a random item weighted by given probabilities.

Referenced by main().