Re: random number

Discussion in 'Python' started by Ian Kelly, Mar 26, 2012.

  1. Ian Kelly

    Ian Kelly Guest

    On Mon, Mar 26, 2012 at 3:24 AM, Michael Poeltl
    <> 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
     
    Ian Kelly, Mar 26, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darren Clark

    Random NOt random?

    Darren Clark, Jun 24, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    459
    mikeb
    Jun 24, 2004
  2. xeys_00
    Replies:
    12
    Views:
    856
    Jonathan Arnold
    Apr 11, 2005
  3. globalrev
    Replies:
    4
    Views:
    773
    Gabriel Genellina
    Apr 20, 2008
  4. Alex Untitled
    Replies:
    11
    Views:
    661
    Giampiero Zanchi
    Nov 16, 2009
  5. VK
    Replies:
    15
    Views:
    1,177
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page