Created
May 10, 2020 11:21
-
-
Save quantumjim/390400644b870dde426ee41dc4e6aa38 to your computer and use it in GitHub Desktop.
Testing rng
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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