Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify the _rand_exp function #23

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Simplify the _rand_exp function

  • Loading branch information
jvoisin committed Dec 31, 2019
commit 222b84c294472c1e144634b1a7e54b2d084bda1a
@@ -45,18 +45,15 @@ def _rand(n):
return 0
return random.randint(0, n-1)

# Exp2 generates n with probability 1/2^(n+1).
@staticmethod
def _rand_exp():
rand_bin = bin(random.randint(0, 2**32-1))[2:]
rand_bin = '0'*(32 - len(rand_bin)) + rand_bin
count = 0
for i in rand_bin:
if i == '0':
count +=1
else:
break
return count
""" Return a number `n` between 0 and 32 included,
with a probability of 1/2^(n+1)
"""
n = random.getrandbits(32)
for i in range(32):
if n % (2 << i):
return i

@staticmethod
def _choose_len(n):
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.