2 OHRRPGCE/FF6 style random item selection (percentage probability based)
5 from ion
.n
.random
import percent
7 """ Illustrates OHRRPGCE's behaviour giving items after battle.
8 XXX make this into an example.
9 Pass a tuple (item, item %, rareitem, rareitem %) as the info parameter.
15 One of (item, rareitem, -1) (where -1 indicates nothing dropped)
21 tmp = chain (((item %, item), (rareitem %, rareitem)))
29 # rareitem's chance to drop:
30 # (100% - item%) * rareitem%
32 # eg. item% == 50, rareitem% == 50
33 # drop item 25% of the time,
34 # rareitem 25% of the time,
35 # nothing 50% of the time.
37 # with rareitem% == 25:
42 if percent ((info
[3] * (100 - info
[1])) / 100 ):
43 return info
[2] # rareitem
47 return -1 # nothing :(
49 # expressed in weighted sampling:
51 # remainder = float (100 - info[1])
53 # return weightedSample ((info[0],info[2], -1), (info[1],
54 # (remainder / 2.0) * (info[3]/100.0), remainder / 2.0)
56 # In more general, weighting is like:
63 # With remainders weighted into the previous remainder space.
66 # say, 50% 40% 30% 20%
70 # 20% (40% * 50%) chance of 2
71 # 8% (30% * (100% - (50% + 20%))) chance of 3
72 # 4.2% chance (20% * (100% - (50% + 20% + 8%)) ) of 4
73 # 16.8% chance (20% * (100% - (50% + 20% + 8% + 4.2%)) ) of None
75 # to be precise (percentages expressed like 1.00 == 100%)
78 # weights.append (factorlist[0])
81 # for fac in factorlist:
82 # weights.append ((fac / 100.0) * (100.0 - sum(weights)))
84 # weights.append (100.0 - sum(weights))
92 # a dictionary mapping number -> name
100 #give item, never rareitem or nothing:
102 print lut
[ got_item ((1, 100, 2, 100))]
106 # give item 50% of the time, rareitem 25% of the time,
107 # or nothing 25% of the time:
109 print lut
[ got_item ((1, 50, 2, 50))]
113 # give item 75% of the time, rareitem 8% of the time,
114 # or nothing 17% of the time:
117 print lut
[ got_item ((1, 75, 2, 33))]