Skip to content

Commit

Permalink
使用新的伪随机算法
Browse files Browse the repository at this point in the history
  • Loading branch information
LYZhelloworld committed Jun 17, 2022
1 parent 43a40a1 commit 389e12f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
24 changes: 14 additions & 10 deletions src/plugins/roulette/pseudorandom.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,30 @@ class RouletteRandomizer:
def __init__(self):
self.ROULETTE_VALUES = (1, 2, 3, 4, 5, 6)
self.ROULETTE_WEIGHTS: defaultdict[int, list[float]] = defaultdict(
lambda: [1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
self.ROULETTE_DELTA = 0.01
self.ROULETTE_MINIMUM_WEIGHT = 0.9
lambda: [1.0/6, 1.0/6, 1.0/6, 1.0/6, 1.0/6, 1.0/6])
self.ROULETTE_PROB_CHOSEN = 0.05

self.ROULETTE_MISS_PROB_BASE = 0.125
self.ROULETTE_MISS_PROB: defaultdict[int, float] = defaultdict(
lambda: self.ROULETTE_MISS_PROB_BASE)
self.ROULETTE_MISS_DELTA = 0.001

def roulette_random(self, group: int) -> int:
'''Returns a value between [1,6]. Reduces the probability a bit when a number is generated.'''
'''Returns a value between [1,6].
The probabilities of chossing values are equal at the beginning.
When a number is chosen, the weight of itself becomes ROULETTE_PROB_CHOSEN,
and the other weights share the remaining probability.'''
result = random.choices(
self.ROULETTE_VALUES, weights=self.ROULETTE_WEIGHTS[group])[0]
index = result - 1 # index of the weight
if self.ROULETTE_WEIGHTS[group][index] > self.ROULETTE_MINIMUM_WEIGHT:
for i in range(len(self.ROULETTE_WEIGHTS[group])):
if i == index:
self.ROULETTE_WEIGHTS[group][i] -= 5*self.ROULETTE_DELTA
else:
self.ROULETTE_WEIGHTS[group][i] += self.ROULETTE_DELTA
for i in range(len(self.ROULETTE_WEIGHTS[group])):
if i == index:
self.ROULETTE_WEIGHTS[group][i] = self.ROULETTE_PROB_CHOSEN
else:
self.ROULETTE_WEIGHTS[group][i] = (
1-self.ROULETTE_PROB_CHOSEN) / 5
return result

def roulette_miss_random(self, group: int) -> bool:
Expand Down
12 changes: 4 additions & 8 deletions tests/plugins/roulette/test_pseudorandom.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ def test_roulette_random(self):
for i in range(len(original_weights)):
actual_weight = roulette.ROULETTE_WEIGHTS[GROUP][i]
if i == index:
expected_weight = original_weights[i] - \
5*roulette.ROULETTE_DELTA
expected_weight = 0.05
else:
expected_weight = original_weights[i] + \
roulette.ROULETTE_DELTA
expected_weight = 0.19
self.assertAlmostEqual(expected_weight, actual_weight)

# test group isolation
Expand All @@ -33,11 +31,9 @@ def test_roulette_random(self):
actual_weight = roulette.ROULETTE_WEIGHTS[OTHER_GROUP][i]
print(i, index)
if i == index:
expected_weight = original_weights_other[i] - \
5*roulette.ROULETTE_DELTA
expected_weight = 0.05
else:
expected_weight = original_weights_other[i] + \
roulette.ROULETTE_DELTA
expected_weight = 0.19
self.assertAlmostEqual(expected_weight, actual_weight)
for i in range(len(original_weights)):
actual_weight = roulette.ROULETTE_WEIGHTS[GROUP][i]
Expand Down

0 comments on commit 389e12f

Please sign in to comment.