-
-
Save changtimwu/dd9f61541f461d482026f6f37311560d to your computer and use it in GitHub Desktop.
| import random, time | |
| from randcrack import RandCrack | |
| import string | |
| random_id_characters = string.ascii_letters + string.digits | |
| random.seed(time.time()) | |
| rc = RandCrack() | |
| for i in range(624): | |
| rc.submit(random.getrandbits(32)) | |
| def gen_random_id(): | |
| return ''.join(random.choice(random_id_characters) for i in range(32)) | |
| def gen_randcrack_id(): | |
| return ''.join( rc.predict_choice(random_id_characters) for i in range(32)) | |
| print("Random result: {}\nCracker result: {}".format(random.randrange(0, 4294967295), rc.predict_randrange(0, 4294967295))) | |
| print("Random result: {}\nCracker result: {}".format( gen_random_id(), gen_randcrack_id() )) | |
genrand_uint32 is called here with k=6
https://github.com/python/cpython/blob/dbe60ee09dc5a624cfb78dff61ecf050a5b3f105/Modules/_randommodule.c#L489
Altough we never call random.seed explicitly but _random.seed(None) is called by default.
https://github.com/python/cpython/blob/dbe60ee09dc5a624cfb78dff61ecf050a5b3f105/Lib/random.py#L125
https://github.com/python/cpython/blob/dbe60ee09dc5a624cfb78dff61ecf050a5b3f105/Lib/random.py#L168
it's actually linux's getrandom system call. That fills the init state Mersenne Twister RNG, which is 624 uint32s. That brings great randomness.
https://github.com/python/cpython/blob/dbe60ee09dc5a624cfb78dff61ecf050a5b3f105/Modules/_randommodule.c#L276
https://github.com/python/cpython/blob/dbe60ee09dc5a624cfb78dff61ecf050a5b3f105/Modules/_randommodule.c#L204
test the underlying C module