performance of script to write very long lines of random chars

Robert Kern robert.kern at gmail.com
Thu Apr 11 07:19:07 EDT 2013


On 2013-04-11 16:20, Steven D'Aprano wrote:
> On Thu, 11 Apr 2013 10:47:43 +0100, Oscar Benjamin wrote:
>
>> On 11 April 2013 08:47, Steven D'Aprano
>> <steve+comp.lang.python at pearwood.info> wrote:
>>
>>> One thing to be aware of: urandom may run out of entropy, and then it
>>> will slow down a lot. If you don't care about cryptographic randomness,
>>> you could use this instead:
>>
>> Reading this I'm realising that I don't really know what os.urandom is.
>> How exactly is it generating random numbers and what do you mean by it
>> running out of entropy?
>
> I am not an expert, but here goes...
>
> Some (most?) modern operating systems provide a cryptographically strong
> source of non-deterministic randomness. The non-deterministic part comes
> from external "stuff", which is called "entropy". Typical sources of
> entropy include network events, user key-presses, moving the mouse, and
> (presumably in machines with special hardware), even thermal noise in
> electrical components.
>
> If the OS hasn't collected enough entropy, urandom can block until it
> has. This can be a problem, e.g. I've experienced issues where scripts
> relying indirectly on urandom that run at system reboot can block for
> minutes at a time, waiting for entropy. If those scripts run before the
> networking software, and before any users log in and start running apps,
> the script can block forever waiting for entropy that never arrives.
>
> (Where "forever" == "70 minute boot times".)
>
> Entropy is used and discarded, so urandom needs the OS to continually
> replenish the amount of entropy. Under normal circumstances, this it
> does, but if you grab lots of urandom output on a system which is
> otherwise quiet and not doing anything, it could run out.
>
> If I've got any of this wrong, corrections will be gratefully acceptable.

Just one important thing: os.urandom() does not block to wait for more entropy. 
Only os.random() does.

http://en.wikipedia.org/wiki//dev/random

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list