Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!news-transit.tcx.org.uk!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '(although': 0.05; 'bits': 0.07; 'python': 0.08; "(i'm": 0.09; 'between.': 0.09; 'inclined': 0.09; 'subject:string': 0.09; 'though:': 0.09; 'underlying': 0.09; 'am,': 0.12; 'received:209.85.210.174': 0.13; 'received:mail- iy0-f174.google.com': 0.13; 'skip:" 40': 0.15; '53-bit': 0.16; 'better?': 0.16; 'discarded': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'padded': 0.16; 'random.': 0.16; 'roy': 0.16; 'mon,': 0.16; 'wrote:': 0.18; 'string,': 0.18; '(which': 0.19; 'dec': 0.22; 'header:In-Reply-To:1': 0.22; 'somewhere': 0.23; 'string': 0.24; "i'm": 0.26; 'function': 0.27; 'random': 0.28; 'message-id:@mail.gmail.com': 0.28; 'assuming': 0.29; "skip:' 30": 0.29; 'looks': 0.29; '(the': 0.30; 'subject:?': 0.31; 'actual': 0.32; "can't": 0.32; 'there': 0.33; 'to:addr :python-list': 0.34; 'probably': 0.34; 'latter': 0.34; 'something': 0.35; 'uses': 0.36; 'two': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.38; 'option': 0.39; "i'd": 0.39; "it's": 0.40; 'received:209': 0.40; 'to:addr:python.org': 0.40; 'huge': 0.61; '2011': 0.61; 'your': 0.61; 'leading': 0.62; 'further': 0.64; 'due': 0.66; '10,000': 0.67; '26,': 0.67; 'safe': 0.70; '12:30': 0.84; 'ugly,': 0.84; 'technique': 0.93; 'task,': 0.95 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=Smu1j8nxbrrUfTSBEi6wFHYZqi6Qny9w4XpA0pAgFFE=; b=nwWALfAjC6QYwDZg+41ABD9kZu3PbHm9Tfmu4Z5fOpYYGchrEAz+SAaYJQziMQ1dco lMHz0bxkEhhJllpiECg6U6Y3CkVfRIL8Fu/rTTpKnr3GkQyFGrtb0RWoaLvlqYNb/Wva 41+SZmimDs6cnku776OltBxpX090bZh0DqdaE= MIME-Version: 1.0 In-Reply-To: References: Date: Mon, 26 Dec 2011 00:50:44 +1100 Subject: Re: Random string of digits? From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324821046 news.xs4all.nl 6988 [2001:888:2000:d::a6]:48493 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17898 On Mon, Dec 26, 2011 at 12:30 AM, Roy Smith wrote: > I want to create a string of 20 random digits (I'm OK with leading > zeros). =A0The 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