fix rand_max in shuffle alg. add note about usage

This commit is contained in:
Danny Ryan 2018-10-03 09:28:42 -05:00
parent 7e978f1b40
commit 86d0c209b7
1 changed files with 6 additions and 2 deletions

View File

@ -289,7 +289,11 @@ Now, a function that shuffles this list:
```python
def shuffle(lst, seed):
assert len(lst) <= MAX_VALIDATOR_COUNT
# entropy is consumed in 3 byte chunks
# rand_max is defined to remove the modulo bias from this entropy source
rand_max = 2**24
assert len(lst) <= rand_max
o = [x for x in lst]
source = seed
i = 0
@ -300,7 +304,7 @@ def shuffle(lst, seed):
remaining = len(lst) - i
if remaining == 0:
break
rand_max = MAX_VALIDATOR_COUNT - MAX_VALIDATOR_COUNT % remaining
rand_max = rand_max - rand_max % remaining
if m < rand_max:
replacement_pos = (m % remaining) + i
o[i], o[replacement_pos] = o[replacement_pos], o[i]