Cryptographically random numbers

Paul Rubin http
Mon Mar 6 18:58:06 EST 2006


"Tuvas" <tuvas21 at gmail.com> writes:
> def s2num(text):
>     if(len(text)==1):
>         return ord(text)
>     else:
>         return ord(text[0])+256*s2num(text[1:])

My favorite way to convert strings to numbers is with binascii:
 from binascii import hexlify
 def s2num(text):
   return int(hexlify(text), 16)

> def cran_rand(min,max):
>     range=int(log(abs(max-min))/log(2))+1

This is kind of ugly and I wonder if it's possible for roundoff error
in the log function to make the answer off by 1, if max-min is very
close to a power of 2.



More information about the Python-list mailing list