Random string of digits?

Roy Smith roy at panix.com
Sun Dec 25 09:21:34 EST 2011


In article <mailman.4068.1324821046.27778.python-list at python.org>,
 Chris Angelico <rosuav at gmail.com> wrote:

> "%020d"%random.randint(0,99999999999999999999)
> (the latter gives you a string, padded with leading zeroes). But I'm
> assuming that you discarded that option due to lack of entropy (ie you
> can't trust randint() over that huge a range).

Actually, the only entropy involved here is the ever increasing amount 
of it between my ears.  It never occurred to me to try that :-)

> For your actual task, I'd be inclined to take ten digits, twice, and
> not bother with join():
> 
> '%010d%010d'%(random.randint(0,9999999999),random.randint(0,9999999999))
> 
> Looks a little ugly, but it works! And only two random number calls
> (which can be expensive).

Hmmm.  In my case, I was looking more to optimize clarity of code, not 
speed.  This is being used during account creation on a web site, so it 
doesn't get run very often.

It turns out, I don't really need 20 digits.  If I can count on

>>> "%020d" % random.randint(0,999999999999999)

to give me 15-ish digits, that's good enough for my needs and I'll 
probably go with that.  Thanks.



More information about the Python-list mailing list