Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '(although': 0.05; 'bits': 0.07; 'override': 0.07; 'roll': 0.07; 'sanity': 0.07; 'python': 0.08; "(i'm": 0.09; 'between.': 0.09; 'inclined': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'subject:string': 0.09; 'though:': 0.09; 'underlying': 0.09; 'am,': 0.12; 'def': 0.13; 'skip:" 40': 0.15; '53-bit': 0.16; 'better?': 0.16; 'discarded': 0.16; 'padded': 0.16; 'random.': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'roy': 0.16; 'mon,': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'string,': 0.18; '(which': 0.19; 'dec': 0.22; 'somewhere': 0.23; 'from:addr:web.de': 0.23; 'string': 0.24; 'code': 0.25; "i'm": 0.26; 'function': 0.27; 'import': 0.27; 'random': 0.28; 'assuming': 0.29; "skip:' 30": 0.29; 'looks': 0.29; 'class': 0.29; 'collections': 0.30; '(the': 0.30; 'chris': 0.30; 'subject:?': 0.31; 'does': 0.32; 'actual': 0.32; "can't": 0.32; 'header:X -Complaints-To:1': 0.33; 'there': 0.33; 'to:addr:python-list': 0.34; 'probably': 0.34; 'latter': 0.34; 'something': 0.35; 'unless': 0.35; 'uses': 0.36; 'two': 0.37; 'but': 0.37; 'run': 0.37; 'skip:_ 10': 0.37; 'enough': 0.38; 'received:org': 0.38; 'option': 0.39; "i'd": 0.39; 'being': 0.39; "it's": 0.40; 'to:addr:python.org': 0.40; 'huge': 0.61; 'range': 0.61; 'according': 0.61; 'quick': 0.61; '2011': 0.61; 'your': 0.61; 'url:index': 0.62; 'leading': 0.62; 'choose': 0.62; 'further': 0.64; 'due': 0.66; '10,000': 0.67; '26,': 0.67; 'supply': 0.69; 'safe': 0.70; 'skip:2 20': 0.71; 'url:php': 0.82; '12:30': 0.84; 'literature': 0.84; 'ugly,': 0.84; 'skip:4 20': 0.93; 'technique': 0.93; 'task,': 0.95 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Random string of digits? Date: Sun, 25 Dec 2011 16:21:49 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084b386.dip.t-dialin.net 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: 68 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324826518 news.xs4all.nl 6842 [2001:888:2000:d::a6]:49035 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17910 Chris Angelico wrote: > 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). 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). Judging from a quick look into the code (method Random._randbelow()) I'd say you don't need to do that unless you override Random.random() and not Random.getrandbits(). Even if you roll your own random() you'll get a warning when you run into the limit: >>> import random >>> random.randrange(10**20) 27709407700486201379L >>> class R(random.Random): ... def random(self): return 4 # * ... >>> R().randrange(10**20) /usr/lib/python2.6/random.py:253: UserWarning: Underlying random() generator does not supply enough bits to choose from a population range this large _warn("Underlying random() generator does not supply \n" 400000000000000000000L (*) According to the literature 4 is the best random number, 9 being the runner-up: http://www.googlefight.com/index.php?lang=en_GB&word1=random+number+dilbert&word2=random+number+xkcd A quick sanity check: >>> from collections import Counter >>> import random >>> Counter(str(random.randrange(10**10000))) Counter({'9': 1060, '6': 1039, '3': 1036, '8': 1007, '7': 997, '4': 977, '1': 976, '5': 976, '2': 970, '0': 962})