Re: [Tutor] True Random Numbers
On 11/2/10, Crallion cralli...@gmail.com wrote: In an attempt to generate true random numbers, 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. The only 'true' source of entropy is a hardware random number generator. For cryptographic work you don't need this. All you need is a seemingly endless unpredictable sequence of bits. For this purpose, a cryptographically secure pseudo random number generator will do nicely. Furthermore, if your intent was for encryption, you shouldn't be getting your entropy over the wire for obvious reasons. (Speed and security.) With your stress on 'true' random numbers, I can only assume your intent is encryption. That said, use the operating system's random number generator. This will vary from system to system in how it is implemented, but all systems have one which is suitable for cryptographic purposes. On *NIX flavors this will be /dev/random. You can either read from this file directly to get a random stream of bits, or use the python standard library to interface with it to do things like get random numbers (as others have pointed out.) Whether reads to this, or calls which use this, will block, waiting for a suitable amount of entropy, depends on the system your code runs on. For example on FreeBSD both '/dev/random' and '/dev/urandom' are always non-blocking. FreeBSD uses a crypto secure 256-bit pseudo random number generator based on yarrow. The same is true for Mac OS X and AIX. On linux '/dev/random' will block if the entropy pool is exhausted, while '/dev/urandom' will not, but is still considered cryptographically secure. (And far better than any home brewed solution.) Also remember, 'pseudo-random' doesn't mean 'almost random,' it means deterministic *if you know the initial state*. (This is only true of some generators though, as some perturb their state with external sources of entropy on a regular basis.) On python you can get random numbers through the operating system's '/dev/urandom' (or on Windows, CryptGenRandom ): # Python example of getting secure, random numbers using the standard library: # This works in python 2.4 and greater on most systems: import random foo = random.SystemRandom() # Uses /dev/urandom or Windows CryptGenRandom # Print 10 random numbers: for i in range(10): foo.random() What are you using the random numbers for? -Modulok- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] True Random Numbers
In an attempt to generate true random numbers, 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. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] True Random Numbers
On 02/11/2010 11:29, Crallion wrote: In an attempt to generate true random numbers, 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, inmodule 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. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor |The urllib2 module has been split across several modules in Python 3.0 named urllib.request and urllib.error so you should be doing `from urllib.request import urlopen`.| -- Kind Regards, Christian Witts ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] True Random Numbers
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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor