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


Groups > comp.lang.python > #22202 > unrolled thread

Re: random number

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2012-03-26 10:48 -0600
Last post2012-03-26 10:48 -0600
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#22202 — Re: random number

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-03-26 10:48 -0600
SubjectRe: random number
Message-ID<mailman.1013.1332780512.3037.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web