1 >>> from ion.n.random import *
3 cumulative_to_simple_weights is a generalization of the OHRRPGCE item reward algorithym.
4 In the OHRRPGCE, an enemy may yield one of two items (or nothing), when defeated.
5 These are the item, and the rare item. Each has an associated percentage.
6 The algorithym is quite simple, and is implemented in the function gotItem()
9 IF percent (item%) THEN give item ELSE IF percent (rareitem%) THEN give rareitem ELSE give nothing.
11 Thus, choosing item% = 50 and rareitem% = 50 gives
13 50% chance of item, 25% chance of raretem, 25% chance of nothing.
15 The principle of taking subsequent percentages as a percentage of the remaining part of 100% is used.
16 Thus, you may use 100% as a percentage; but only once.
17 [100, foo, bar] # foo and bar don't matter (the chance of them is 0%, regardless what values you assign)
18 [foo,bar, 100] # if foo-item or bar-item aren't chosen, then 100-item is guaranteed.
20 cumulative_to_simple_weights converts the cumulative-percentages format into straight weights.
22 >>> cumulative_to_simple_weights ([50.0,50.0])
25 The final item (chance of Nothing) is omitted if it is 0%:
27 >>> cumulative_to_simple_weights ([50.0, 100.0])
30 In this case the last item is effectively the default (rather than Nothing)
34 dispense is used for situations where you wish to dispense items randomly from a given 'stock' - thus, it is guaranteed that until the 'box' is emptied, no more of an item may be dispensed than the number initially in it.
36 >>> box = ['gold', 'malachite', 'topaz']
38 You need a status sequence, where temporary status information can be kept. It needs only to
39 support seq.extend() and seq.append()
43 dispense is similar to boxfill; dispense is forced to dispense only items never dispensed before, where boxfill simply weights probability toward dispensing fresh items.
45 >>> item = dispense (box, status)
49 If you try to take out more items than are currently available, Nones are returned in their place.
51 >>> items = dispense (box, status, n = 3)
58 After emptying, the box starts out full again.
60 >>> item = dispense (box, status)
67 XXX gotitem; samples; choices; boxfill