Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'assign': 0.04; 'happily': 0.07; 'used.': 0.07; "(i'm": 0.09; 'subject:string': 0.09; 'tags,': 0.09; 'am,': 0.12; 'received:209.85.210.174': 0.13; 'received:mail-iy0-f174.google.com': 0.13; 'better?': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'instance;': 0.16; 'roy': 0.16; 'mon,': 0.16; 'wrote:': 0.18; 'dec': 0.22; 'header:In-Reply-To:1': 0.22; 'objects,': 0.23; 'string': 0.24; 'random': 0.28; 'message-id:@mail.gmail.com': 0.28; 'sun,': 0.30; 'specified': 0.31; 'subject:?': 0.31; 'sort': 0.33; 'actually': 0.33; 'there': 0.33; 'to:addr:python-list': 0.34; 'size,': 0.34; 'something': 0.35; 'acceptable': 0.37; 'combination': 0.37; 'received:google.com': 0.37; 'steven': 0.38; 'received:209.85': 0.38; 'should': 0.39; 'received:209': 0.40; 'to:addr:python.org': 0.40; '2011': 0.61; 'leading': 0.62; '26,': 0.67; 'alphanumeric': 0.67; 'million': 0.76; '-0500,': 0.84; '12:48': 0.84; 'technique': 0.93; 'absolutely': 0.98 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=I51P+B+IOAJpC+uw2aTV4w/Bbcip9zKfDwfuloiC0S8=; b=BMOmJulFozcxMBJi5yX5wb9IFpLx4kNT2Cp8oYkeoB+1oWiELHNZV6ayM8On3pAwhP On+jRaKDGy+UNpgrdXiwQOToEv1dg1wQy+ip6AfxNb1Kk687IZvtr1uD87tapnTReo0X p32E+hIF3yrOkz9jh8ViS6pW7wcAu7wD0Lix8= MIME-Version: 1.0 In-Reply-To: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> Date: Mon, 26 Dec 2011 00:54:40 +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: 23 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324821283 news.xs4all.nl 6843 [2001:888:2000:d::a6]:45812 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17900 On Mon, Dec 26, 2011 at 12:48 AM, Steven D'Aprano wrote: > On Sun, 25 Dec 2011 08:30:46 -0500, 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? > > '%20d' % random.randint(0, 10**20-1) I should mention that this sort of thing is absolutely acceptable in situations where you don't actually need that level of randomness; if you want to assign 20-digit numbers to each of ten million objects, you can happily use something that's only really random to fifteen. I use this technique with base 36 numbers to generate arbitrary alphanumeric strings to use as message tags, for instance; fill out a specified field size, even though not every combination will actually be used. ChrisA