![]() |
Python
1.0
|
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... | |
Return random numbers weighted by given probabilities.
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)]
l | list of items. |
weights | list of probabilities. |
Referenced by main().
def weightedNumGenerator.genWeightedListD | ( | d | ) |
Using a dictionary, instead of two lists.
d | pairs of items,probabilities. |
Referenced by main().
def weightedNumGenerator.main | ( | ) |
Main program for testing.
References genWeightedList(), genWeightedListD(), weightedRand(), weightedRand2(), and weightedRandD().
def weightedNumGenerator.weightedRand | ( | seq | ) |
Return a random element from seq (same of choice from random).
seq | given sequence of items. |
Referenced by main().
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).
TODO - make method more efficient with quicksort
l | list of items |
weights | list of probabilities. |
Referenced by main().
def weightedNumGenerator.weightedRandD | ( | d | ) |
Using a dictionary.
d | pairs of items,probabilities. |
Referenced by main().