[Python-Dev] os.urandom API (original) (raw)
Raymond Hettinger raymond.hettinger at verizon.net
Sun Aug 29 22:37:17 CEST 2004
- Previous message: [Python-Dev] Re: python/dist/src/Lib/lib-tk tkFont.py,1.6,1.7
- Next message: [Python-Dev] os.urandom API
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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:
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).
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.
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.
The decimal module supports instantiation directly from long integers but not from binary strings.
In favor of a string of bytes:
- This form is handy for cyptoweenies to xor with other byte strings (perhaps for a one-time pad).
Raymond
- Previous message: [Python-Dev] Re: python/dist/src/Lib/lib-tk tkFont.py,1.6,1.7
- Next message: [Python-Dev] os.urandom API
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]