Re: [Tutor] True Random Numbers

2010-11-03 Thread Modulok
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] Displaying picture and Text

2010-11-03 Thread patty
Hi - I am running Python 2.6.6 on my HP netbook with Windows 7. The 
default picture viewer is set to HP Photo Viewer.  I am working on a part
of my python program where a picture is supposed to display with a few
lines of text below it.  Ideally, I would like this to stay on the screen
for  a short  period of time (2 minutes?) but I am using raw_input() for
now.

I imported the Image library and when I run the program below, it brings
up a window (stdout or python?) with cursor flashing for a few seconds and
then the HP Photo Viewer comes up with the picture, I close this window
and then my text comes up in a python window.

import Image

fhdl = Image.open(C:\Users\StarShip\PyProgs\\bbsparkle.gif)
fhdl.show()
print here is some test text test text test text
print here is some more test text test text test text
print here is even more of some test text test text test text

raw_input(press any key to continue)

How do I get any picture to display on half a screen then my print text
display below that and stay on screen for 2 or 3 minutes before I call
another function that will redraw the screen anyway - it will be
interactive text responses with the user.  Also this is a test .gif file,
I may want to just edit the source file and change this to a .jpg.

I don't want to change the system defaults for this, maybe for the picture
itself if that is necessary.

Thanks

Patty

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor