Cryptographically random numbers

Emile van Sebille emile at fenx.com
Sun Mar 5 16:26:03 EST 2006


> def cran_rand(min,max):

You're shadowing built-ins here.  Not a problem, but something I'd generally 
avoid.


>    if(min>max):
>        x=max
>        max=min
>        min=x

If the args were a,b you could say:

maxarg,minarg = min(a,b),max(a,b)


>    range=round(log(max-min)/log(256))

more builtin shadowing...

>    if range==0:
>        range=1

or as alt_name_for_range=...  or 1

>    num=max+1
>    while(num>max):
>        num=min+s2num(urandom(range))

I'll assume os.urandom is urandom.  What's s2num?

>    return num
>
> Any comments on this? I think it should hold up to a test, it seems to
> work alright. Thanks!
>


If this does what you want, that's good.  But someday when you look again 
and see

while(num>max): num=min+s2num(urandom(range))

you'll wonder...

Thankfully-python-uses-int-and-not-num-ly y'rs,

Emile van Sebille
emile at fenx.com










More information about the Python-list mailing list