Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #17898
| 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 | <rosuav@gmail.com> |
| 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 | <roy-EDF7D2.08304625122011@news.panix.com> |
| References | <roy-EDF7D2.08304625122011@news.panix.com> |
| Date | Mon, 26 Dec 2011 00:50:44 +1100 |
| Subject | Re: Random string of digits? |
| From | Chris Angelico <rosuav@gmail.com> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4068.1324821046.27778.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
On Mon, Dec 26, 2011 at 12:30 AM, Roy Smith <roy@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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Random string of digits? Roy Smith <roy@panix.com> - 2011-12-25 08:30 -0500
Re: Random string of digits? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-25 13:48 +0000
Re: Random string of digits? Chris Angelico <rosuav@gmail.com> - 2011-12-26 00:54 +1100
Re: Random string of digits? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-25 15:46 +0000
Re: Random string of digits? Chris Angelico <rosuav@gmail.com> - 2011-12-26 03:11 +1100
Re: Random string of digits? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-25 17:18 +0000
Re: Random string of digits? Roy Smith <roy@panix.com> - 2011-12-25 12:41 -0500
Re: Random string of digits? 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-25 11:50 -0800
Re: Random string of digits? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-26 03:00 +0000
Re: Random string of digits? Chris Angelico <rosuav@gmail.com> - 2011-12-26 14:43 +1100
Re: Random string of digits? Roy Smith <roy@panix.com> - 2011-12-25 23:17 -0500
Re: Random string of digits? Chris Angelico <rosuav@gmail.com> - 2011-12-26 15:26 +1100
Re: Random string of digits? Serhiy Storchaka <storchaka@gmail.com> - 2011-12-25 19:32 +0200
Re: Random string of digits? Chris Angelico <rosuav@gmail.com> - 2011-12-26 00:50 +1100
Re: Random string of digits? Roy Smith <roy@panix.com> - 2011-12-25 09:21 -0500
Re: Random string of digits? Chris Angelico <rosuav@gmail.com> - 2011-12-26 01:51 +1100
Re: Random string of digits? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-25 15:27 +0000
Re: Random string of digits? Chris Angelico <rosuav@gmail.com> - 2011-12-26 00:51 +1100
Re: Random string of digits? Peter Otten <__peter__@web.de> - 2011-12-25 16:21 +0100
csiph-web