Skip to content

Instantly share code, notes, and snippets.

@quantumjim
Created May 10, 2020 11:21
Show Gist options
  • Select an option

  • Save quantumjim/390400644b870dde426ee41dc4e6aa38 to your computer and use it in GitHub Desktop.

Select an option

Save quantumjim/390400644b870dde426ee41dc4e6aa38 to your computer and use it in GitHub Desktop.
Testing rng
import secrets
import numpy as np
samples = 1000000
zs = 0
os = 0
number_list = []
for _ in range(samples):
n = 10
p0 = 0.6
m = 10
string = ''
for j in range(n):
r = secrets.randbits(m)
if r<(p0*2**m):
bit = '0'
zs += 1
else:
bit = '1'
os += 1
string += bit
number_list.append( str(int(string,2)) )
print('Actual fraction of 0s is')
print(zs/(os+zs))
with open('biased_secure.txt', 'w') as file:
file.write('\n'.join(str(number) for number in number_list))
import secrets
import numpy as np
samples = 1000000
number_list = []
for _ in range(samples):
number_list.append( secrets.randbits(n) )
with open('unbiased_secure.txt', 'w') as file:
file.write('\n'.join(str(number) for number in number_list))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment