Cryptographically random numbers

Tuvas tuvas21 at gmail.com
Mon Mar 6 23:49:03 EST 2006


Wait, I now see that there is a native base 2 log in python, so I will
just do that rather than my adhoc way. The reason for adding one is to
make sure there isn't any problems if the log is, for instance, 2.2. It
will always round up. It's better to have to try twice to make sure the
number can have the max range than never use the top half, as the first
version did... That changes the function to:

def cran_rand(min,max):
    range=int(log(abs(max-min),2))+1
    num=max+1
    if range%8==0:
        crange=range/8
    else:
        crange=range/8+1
    while(num>max):
        num=min+s2num(urandom(crange))%(2**range)
    return num

As to the s2num(text), well, that looks really neat. Is there an easy
way to do the reverse of that? Thanks!




More information about the Python-list mailing list