Truly random numbers

Mark McEahern marklists at mceahern.com
Wed Feb 12 01:10:44 CET 2003


> [...]
> it, convert it into binary and then do it what ever I want. Anyway, what
> I need is a list of n groups of XXXX ( four digits ) numbers, and I
> solved it through the list, but can I ( I know I can, but I don't know
> how ) somehow generate this list from /dev/random?

Does this work?

#!/usr/bin/env python

from __future__ import generators

def random_characters(count, valid_characters):
    valid_characters = map(str, valid_characters)
    f = file('/dev/urandom')
    for x in xrange(count):
        while True:
            candidate = f.read(1)
            if candidate in valid_characters:
                yield candidate
                break

for x in random_characters(10, range(10)):
    print x

// m
-






More information about the Python-list mailing list