5.2.3. CDFChooser¶
This file defines a single class which is an instance of ChooserBase to work on CDF lines.
- class CDFChooser.CDFChooser(dist='uniform', args=(), seed=None)¶
This is an instance of ChooserBase that works on cumulative distribution function (CDF) lines. These lines contain weights and values where each weight is the difference between its current number and the previous one. The final weight should always be 1.0 and every weight should be between on (0.0, 1.0]. These weights allow the values to be picked at random, but with a given likelihood instead of each being equally likely.
Parameters:
dist: the distribution to use when picking random numbers, default is uniform
args: any arguments that need to be passed to initialize the distribution
seed: optionally the seed to initialize the random number generator with
Example CDF Lines:
0.2 www.zulu.isp, 0.6 aahz.butts.finl, 1.0 zippy.samnos.world: choose www.zulu.isp 20% of the time, aahz.butts.finl 40% of the time and zippy.samnos.world 40% of the time
.33 email, .4 word, .6 shell, .7 browser, 1.0 spreadsheet: choose email 33% of the time, word 7% of the time, shell 20% of the time, browser 10% of the time and spreadsheet 30%
Example usage: > cdf = CDFChooser( seed=592091 ) > cdf.parse_line( “.33 email, .4 word, .6 shell, .7 browser, 1.0 spreadsheet” ) > cdf.next_choice()
email
- > cdf.next_choice()
spreadsheet
- > cdf.next_choice()
spreadsheet
- next_choice()¶
Return: the next choice
- num_choices()¶
Return: the number of choices that make up this CDF
- parse_line(line, func=None)¶
Given a line of text that is comma separated where each value is a space separated CDF value and the name of the associated transition, parse that line
Parameters:
line: the line to parse
func: if not None this is a function that will be passed the key from each line and whose return value will be stored as the actual key
Examples:
cdf.parse_line( “.33 email, .4 word, .6 shell, .7 browser, 1.0 spreadsheet” )
cdf.parse_line( ‘0.2 www.zulu.isp, 0.6 aahz.butts.finl, 1.0 zippy.samnos.world’ )
- CDFChooser.test()¶
Run a unittest suite for the CDF Chooser.