-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomEntry.py
48 lines (40 loc) · 1.42 KB
/
RandomEntry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import random
class WeightedMap(object):
def __init__(self):
self.totalWeight = 0
self.entries = {}
def add(self, entry, weight):
oldWeight = self.entries.get(entry, 0)
self.entries[entry] = weight
self.totalWeight += weight - oldWeight
def get(self, entry):
return self.entries.get(entry, 0)
def randomGet(self):
rand = random.random()
print("Random number " + str(rand))
lastNearestRand = 1.1
lastNearestEntry = None
probs = {}
for eachKey in self.entries.keys():
eachProb = self.entries[eachKey] / self.totalWeight
probs[eachProb] = eachKey
sortedProbs = sorted(probs.keys())
accProbs = {}
accProbs[sortedProbs[0]] = probs[sortedProbs[0]]
runningAccRand = sortedProbs[0]
for i in range(1, len(sortedProbs)):
accProbs[sortedProbs[i]+runningAccRand] = probs[sortedProbs[i]]
runningAccRand += sortedProbs[i]
for eachAccProb in accProbs.keys():
if eachAccProb > rand:
if abs(eachAccProb-rand) < abs(lastNearestRand-rand):
lastNearestRand = eachAccProb
lastNearestEntry = accProbs[eachAccProb]
return lastNearestEntry
test = WeightedMap()
test.add("Goog", 20)
test.add("Amzn", 10)
test.add("Msft", 30)
test.add("Appl", 15)
test.add("Att", 25)
print(test.randomGet())