Random string of digits?

Chris Angelico rosuav at gmail.com
Sun Dec 25 08:50:44 EST 2011


On Mon, Dec 26, 2011 at 12:30 AM, Roy Smith <roy at panix.com> wrote:
> I want to create a string of 20 random digits (I'm OK with leading
> zeros).  The best I came up with is:
>
> ''.join(str(random.randint(0, 9)) for i in range(20))
>
> Is there something better?

The simple option is:
random.randint(0,99999999999999999999)
or
"%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).

The way I'd do it would be in chunks. The simple option is one chunk;
your original technique is twenty. We can go somewhere in between.
First thing to do though: ascertain how far randint() is properly
random. The Python 2 docs [1] say that the underlying random()
function uses 53-bit floats, so you can probably rely on about that
much randomness; for argument's sake, let's say it's safe for up to
10,000 but no further (although 53 bits give you about 15 decimal
digits).

''.join('%04d'%random.randint(0,9999) for i in range(5))

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).

ChrisA



More information about the Python-list mailing list