Truly random numbers

John La Rooy nospampls.jlr at doctor.com
Tue Feb 11 21:08:56 EST 2003


On Wed, 12 Feb 2003 12:57:17 +1100
John La Rooy <nospampls.jlr at doctor.com> wrote:

> 
> ...or use select/poll
> 
> 
>     import os,select
> 
>     number_of_groups=10000
>     length_of_group=4
> 
>     dev_random=os.open("/dev/random",os.O_RDONLY)
>     P=select.poll()
>     P.register(dev_random,select.POLLIN)
> 
>     groups=[]
> 
>     while len(groups) < number_of_groups:
>         group=""
>         while len(group) < length_of_group:
>             if P.poll(0.1):
>                 group+="%2.2X"%ord(os.read(dev_random,1))
>         groups.append(group)
>     print groups
> 
> 

or more simply (not using os.open() and os.read())


    import select

    number_of_groups=10000
    length_of_group=4

    dev_random=open("/dev/random")
    P=select.poll()
    P.register(dev_random.fileno(),select.POLLIN)

    groups=[]

    while len(groups) < number_of_groups:
        group=""
        while len(group) < length_of_group:
            if P.poll(0.1):
                group+="%2.2X"%ord(dev_random.read(1))
        groups.append(group)
    print groups

John




More information about the Python-list mailing list