dimanche 13 décembre 2015

Why does Python's getrandbits() never produce negative numbers?

I have a python script that uses both random.Random and random.SystemRandom. I use the SystemRandom object to seed the Random object. The values from the Random object are used in a simulation and the seed value is printed for the user to reproduce the results.

However, I've noticed that with either of them, if I call getrandbits(64), it never displays the number as negative. It's always a positive integer.

I would expect, given randomness, that close to 50% of the time it would produce a negative value (by setting the left-most bit to 1).

Why are my expectations incorrect?

My implementation:

sr = random.SystemRandom()
seed = sr.getrandbits(64)
seedgen = random.Random(seed)
...
seedgen.getrandbits(64)




Aucun commentaire:

Enregistrer un commentaire