Module wchoice
[hide private]
[frames] | no frames]

Module wchoice

source code

wchoice.py -- by bearophile, V.1.0 Oct 30 2006

Weighted choice: like the random.choice() when the probabilities of the single elements aren't the same.

Functions [hide private]
 
wchoice(objects, frequences, filter=True, normalize=True)
wchoice(objects, frequences, filter=True, normalize=True): return a function that return the given objects with the specified frequency distribution.
source code
Variables [hide private]
  __package__ = None
Function Details [hide private]

wchoice(objects, frequences, filter=True, normalize=True)

source code 

wchoice(objects, frequences, filter=True, normalize=True): return a function that return the given objects with the specified frequency distribution. If no objects with frequency>0 are given, return a constant function that return None.

Input: objects: sequence of elements to choose. frequences: sequence of their frequences. filter=False disables the filtering, speeding up the object creation, but less bad cases are controlled. Frequences must be float > 0. normalize=False disables the probablitity normalization. The choice becomes faster, but sum(frequences) must be 1