Advertisement
brandon15811

Untitled

Nov 23rd, 2011
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. from random import random
  2.  
  3. class WeightedChoice(object):
  4. def __init__(self, weights):
  5. """Pick items with weighted probabilities.
  6.  
  7. weights
  8. a sequence of tuples of item and it's weight.
  9. """
  10. self._total_weight = 0.
  11. self._item_levels = []
  12. for item, weight in weights:
  13. self._total_weight += weight
  14. self._item_levels.append((self._total_weight, item))
  15.  
  16. def pick(self):
  17. pick = self._total_weight * random()
  18. for level, item in self._item_levels:
  19. if level >= pick:
  20. return item
  21.  
  22. import csv
  23.  
  24. weighed_items = [(item,float(weight)) for item,weight in csv.reader(open('file.csv'))]
  25. picker = WeightedChoice(weighed_items)
  26. print(picker.pick())
  27.  
  28. lst = [ ('Orange', 0.10), ('Apple', 0.05), ('Mango', 0.15), ('etc', 0.69) ]
  29.  
  30. x = 0.0
  31. lst2 = []
  32. for fruit, chance in lst:
  33. tup = (x, fruit)
  34. lst2.append(tup)
  35. x += chance
  36.  
  37. tup = (x, None)
  38. lst2.append(tup)
  39.  
  40. import random
  41.  
  42. def pick_one(lst2):
  43. if lst2[0][1] is None:
  44. raise ValueError, "no valid values to choose"
  45. while True:
  46. r = random.random()
  47. for x, fruit in reversed(lst2):
  48. if x <= r:
  49. if fruit is None:
  50. break # try again with a different random value
  51. else:
  52. return fruit
  53.  
  54. pick_one(lst2)
  55.  
  56. def choose(items,chances):
  57. import random
  58. p = chances[0]
  59. x = random.random()
  60. i = 0
  61. while x > p :
  62. i = i + 1
  63. p = p + chances[i]
  64. return items[i]
  65.  
  66. lst = [ ('Orange', 0.10), ('Apple', 0.05), ('Mango', 0.15), ('etc', 0.69) ]
  67.  
  68. x = 0.0
  69. lst2 = []
  70. for fruit, chance in lst:
  71. low = x
  72. high = x + chance
  73. tup = (low, high, fruit)
  74. lst2.append(tup)
  75. x += chance
  76.  
  77. if x > 1.0:
  78. raise ValueError, "chances add up to more than 100%"
  79.  
  80. low = x
  81. high = 1.0
  82. tup = (low, high, None)
  83. lst2.append(tup)
  84.  
  85. import random
  86.  
  87. def pick_one(lst2):
  88. if lst2[0][2] is None:
  89. raise ValueError, "no valid values to choose"
  90. while True:
  91. r = random.random()
  92. for low, high, fruit in lst2:
  93. if low <= r < high:
  94. if fruit is None:
  95. break # try again with a different random value
  96. else:
  97. return fruit
  98.  
  99. pick_one(lst2)
  100.  
  101.  
  102. # test it 10,000 times
  103. d = {}
  104. for i in xrange(10000):
  105. x = pick_one(lst2)
  106. if x in d:
  107. d[x] += 1
  108. else:
  109. d[x] = 1
  110.  
  111. import random
  112. d= {'orange': 0.10, 'mango': 0.15, 'apple': 0.05}
  113. weightedArray = []
  114. for k in d:
  115. weightedArray+=[k]*int(d[k]*100)
  116. random.choice(weightedArray)
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement