Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #22202

Re: random number

Path csiph.com!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 <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.013
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; '>>>>': 0.09; 'parameter': 0.09; 'received:mail-lpp01m010-f46.google.com': 0.09; 'subject:number': 0.09; 'cc:addr:python-list': 0.15; 'subject:random': 0.16; "skip:' 30": 0.18; 'mon,': 0.18; 'string': 0.18; 'def': 0.20; 'wrote:': 0.21; 'header:In-Reply-To:1': 0.22; 'cc:addr:gmail.com': 0.23; 'received:209.85.215.46': 0.23; 'import': 0.24; 'cc:no real name:2**0': 0.26; 'cc:2**1': 0.26; 'message-id:@mail.gmail.com': 0.27; 'cc:addr:python.org': 0.27; 'skip:( 20': 0.27; 'cheers,': 0.28; 'received:209.85': 0.32; 'received:google.com': 0.32; 'received:209.85.215': 0.34; 'skip:s 20': 0.34; 'probably': 0.34; "skip:' 10": 0.35; 'received:209': 0.35; 'characters': 0.35; 'list': 0.37; 'michael': 0.38; 'skip:l 20': 0.38; 'mar': 0.61; '26,': 0.66; '2012': 0.69; 'random,': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=bsqrGOGabC6YQxbfvymJl6Xbzw/KvkYaBrILt+3i/qM=; b=dDGQcIa26xdiR+lXb8VyNwpMZXIj+nZgPlorkagbXponacUtXnW8+l0WJl16/ifjhW L5bZ7zCLWV0VbRdfW9lSaNz7t0ZaA4HEErncnOqeoQwyAaZqWZRxjpsfdAUXqPCfw7Fg CSeZbB0c3jxY2QSxVAFY2ZMJiE5d/nXEniYsB3TGVcjH9XOu3tlgdxfRkCsPcn4iY+j4 IJUzJbxyjnPPm1QGVrw/QmgIGKp7vf7U2mybAENIA1fyZKTgiP+QM1JHn22tNMRo6T+B 4M8oHjAM09tMaM9YRbDW0EW/7yAjzdetDuv5zKBsoemdlAbRjf1q19Y/+d0kGseJnsYM +eWg==
MIME-Version 1.0
In-Reply-To <20120326092403.GB22725@terra.cms.at>
References <CABgq=FyhR+Ldujj3YKRBpVXRVoeoayuXZviUUcNx-gXnFiHLSw@mail.gmail.com> <20120326064000.GB19865@terra.cms.at> <CABgq=FwMuHR1nJiaCDiTJAE4Y_3y6WRhZ2DOMaYYC=ex_gT93g@mail.gmail.com> <20120326092403.GB22725@terra.cms.at>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Mon, 26 Mar 2012 10:48:01 -0600
Subject Re: random number
To Michael Poeltl <michael.poeltl@univie.ac.at>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org, Nikhil Verma <varma.nikhil22@gmail.com>
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.1013.1332780512.3037.python-list@python.org> (permalink)
Lines 23
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1332780512 news.xs4all.nl 6909 [2001:888:2000:d::a6]:37174
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:22202

Show key headers only | View raw


On Mon, Mar 26, 2012 at 3:24 AM, Michael Poeltl
<michael.poeltl@univie.ac.at> wrote:
>>>> import random, string
>>>> def random_number(id):
> ...     characters = list(string.ascii_lowercase +
> ...                       string.ascii_uppercase +
> ...                       string.digits)
> ...     coll_rand = []
> ...     for i in range(6):
> ...         random.shuffle(characters)
> ...         coll_rand.append(characters[0])
> ...     return ''.join(coll_rand)

You don't need to do all that list manipulation.  This is probably quicker:

def random_number():  # Unused "id" parameter omitted
    characters = (string.ascii_lowercase +
                  string.ascii_uppercase +
                  string.digits)
    return ''.join(random.choice(characters) for i in range(6))

Cheers,
Ian

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: random number Ian Kelly <ian.g.kelly@gmail.com> - 2012-03-26 10:48 -0600

csiph-web