[Tutor] True Random Numbers

Steven D'Aprano steve at pearwood.info
Tue Nov 2 11:16:25 CET 2010


Crallion wrote:
> In an attempt to generate "true" random numbers, 

Have you considered using your operating system's source of random numbers?

random.SystemRandom and os.urandom return numbers which are 
cryptographically sound. They are also based on various sources of 
physical randomness (which is sometimes a disadvantage, as the entropy 
can run out and then the functions will hang waiting for more entropy to 
be collected).


 > I found this python script- http://code.google.com/p/truerandom/
> Getting to the point, I get this error when I run my program.
> 
> File "/dev/python/absolute/truerandom.py", line 9, in <module>
>     from urllib import urlopen
> ImportError: cannot import name urlopen
> 
> Is urllib and/or urlopen deprecated in 3.1.2 or is it something I forgot to do?
> Python is running on OSX 10.6, which I upgraded from the default install to 3.1.2.

You should never upgrade the system Python by hand, because parts of the 
OS are likely to expect a specific version. You should install it 
separately.

But in this case, something is screwy, because that script is written 
for Python 2.x and won't run at all under Python 3.x. The fact that you 
managed to get as far as an ImportError is weird -- it should fail with 
SyntaxError during compilation.

urllib still exists in Python 3.x, but it has been re-organised and 
urlopen is now called something else. Instead of

from urllib import urlopen

you need

from urllib.request import urlopen

See the Fine Manual for further details.


-- 
Steven



More information about the Tutor mailing list