random number

Ian Kelly ian.g.kelly at gmail.com
Mon Mar 26 18:48:01 CEST 2012


On Mon, Mar 26, 2012 at 3:24 AM, Michael Poeltl
<michael.poeltl at 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



More information about the Python-list mailing list