[Python-Dev] os.urandom API
raymond.hettinger at verizon.net
Sun Aug 29 22:37:17 CEST 2004
I would like to change the API for the new os.urandom(n) function to
return a long integer instead of a string. The former better serves
more use cases and fits better with existing modules.
In favor of a long integer:
1) The call random.seed(os.random(100)) is a likely use case. If the
intermediate value is a string, then random.seed() will hash it and only
use 32 bits. If the intermediate value is a long integer, all bits are
used. In the given example, the latter is clearly what the user expects
(otherwise, they would only request 4 bytes).
2) Another likely use case is accessing all the tools in the random
module with a subclass that overrides random() and getrandbits(). Both
can be done easier and faster if os.random() returns long integers. If
the starting point is a string, the code gets ugly and slow.
3) Most use cases for random values involve numeric manipulation.
Simple tasks like finding a random integer in the range [0,100000)
become unnecessarily more complicated when starting from a string.
4) The decimal module supports instantiation directly from long integers
but not from binary strings.
In favor of a string of bytes:
1) This form is handy for cyptoweenies to xor with other byte strings
(perhaps for a one-time pad).
More information about the Python-Dev