Cryptographically random numbers

Tuvas tuvas21 at gmail.com
Mon Mar 6 18:53:48 EST 2006


Ahh, you are correct, that is a large bug... How about this one?

def s2num(text):
    if(len(text)==1):
        return ord(text)
    else:
        return ord(text[0])+256*s2num(text[1:])

def cran_rand(min,max):
    range=int(log(abs(max-min))/log(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




More information about the Python-list mailing list