generate random digits with length of 5

Discussion in 'Python' started by sotirac, Sep 28, 2008.

  1. sotirac

    sotirac Guest

    Wondering if there is a better way to generate string of numbers with
    a length of 5 which also can have a 0 in the front of the number.


    <pre>
    random_number = random.sample([0,1,2,3,4,5,6,7,8,9], 5) # choose 5
    elements
    code = 'this is a string' + str(random_number[0]) +
    str(random_number[1]) + str(random_number[2]) + str(random_number[3])
    + str(random_number[4])
    </pre>
     
    sotirac, Sep 28, 2008
    #1
    1. Advertising

  2. sotirac

    Chris Rebert Guest

    On Sun, Sep 28, 2008 at 12:59 PM, sotirac <> wrote:
    > Wondering if there is a better way to generate string of numbers with
    > a length of 5 which also can have a 0 in the front of the number.
    >
    >
    > <pre>
    > random_number = random.sample([0,1,2,3,4,5,6,7,8,9], 5) # choose 5
    > elements
    > code = 'this is a string' + str(random_number[0]) +
    > str(random_number[1]) + str(random_number[2]) + str(random_number[3])
    > + str(random_number[4])


    code = ''.join(str(digit) for digit in random_number)

    Regards,
    Chris

    > </pre>
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >

    --
    Follow the path of the Iguana...
    http://rebertia.com
     
    Chris Rebert, Sep 28, 2008
    #2
    1. Advertising

  3. On Sep 28, 2:59 pm, sotirac <> wrote:
    > Wondering if there is a better way to generate string of numbers with
    > a length of 5 which also can have a 0 in the front of the number.
    >
    > <pre>
    >  random_number = random.sample([0,1,2,3,4,5,6,7,8,9], 5) # choose 5
    > elements
    >  code = 'this is a string' + str(random_number[0]) +
    > str(random_number[1]) + str(random_number[2]) + str(random_number[3])
    > + str(random_number[4])
    > </pre>


    '%05i'%random.randint(0,99999)
     
    Aaron \Castironpi\ Brady, Sep 28, 2008
    #3
  4. Chris Rebert wrote:
    > On Sun, Sep 28, 2008 at 12:59 PM, sotirac <> wrote:
    >
    >> Wondering if there is a better way to generate string of numbers with
    >> a length of 5 which also can have a 0 in the front of the number.
    >>
    >>
    >> <pre>
    >> random_number = random.sample([0,1,2,3,4,5,6,7,8,9], 5) # choose 5
    >> elements
    >> code = 'this is a string' + str(random_number[0]) +
    >> str(random_number[1]) + str(random_number[2]) + str(random_number[3])
    >> + str(random_number[4])
    >>

    >
    > code = ''.join(str(digit) for digit in random_number)
    >
    > Regards,
    > Chris
    >
    >
    >> </pre>
    >>
    >> --
    >> http://mail.python.org/mailman/listinfo/python-list
    >>
    >>

    will random.randint(10000,99999) work for you?
     
    Gary M. Josack, Sep 28, 2008
    #4
  5. Aaron "Castironpi" Brady wrote:
    > On Sep 28, 2:59 pm, sotirac <> wrote:
    >
    >> Wondering if there is a better way to generate string of numbers with
    >> a length of 5 which also can have a 0 in the front of the number.
    >>
    >> <pre>
    >> random_number = random.sample([0,1,2,3,4,5,6,7,8,9], 5) # choose 5
    >> elements
    >> code = 'this is a string' + str(random_number[0]) +
    >> str(random_number[1]) + str(random_number[2]) + str(random_number[3])
    >> + str(random_number[4])
    >> </pre>
    >>

    >
    > '%05i'%random.randint(0,99999)
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >

    This produces numbers other than 5 digit numbers. making the start
    number 10000 should be fine.
     
    Gary M. Josack, Sep 28, 2008
    #5
  6. sotirac

    Guest

    sotirac:
    > random_number = random.sample([0,1,2,3,4,5,6,7,8,9], 5) # choose 5 elements


    But note that's without replacement. So if you want really random
    numbers you can do this:

    >>> from string import digits
    >>> from random import choice
    >>> "".join(choice(digits) for d in xrange(5))

    '93898'

    If you need more speed you can invent other solutions, like (but I
    don't know if it's faster):

    >>> from random import shuffle
    >>> ldigits = list(digits)
    >>> ldigits

    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    >>> shuffle(ldigits)
    >>> ldigits

    ['3', '8', '6', '4', '9', '7', '5', '2', '0', '1']
    >>> "".join(ldigits[:5])

    '38649'

    But this may be the most efficient way:

    >>> from random import randrange
    >>> str(randrange(100000)).zfill(5)

    '37802'

    Bye,
    bearophile
     
    , Sep 28, 2008
    #6
  7. Gary M. Josack wrote:
    > Aaron "Castironpi" Brady wrote:
    >> On Sep 28, 2:59 pm, sotirac <> wrote:
    >>
    >>> Wondering if there is a better way to generate string of numbers with
    >>> a length of 5 which also can have a 0 in the front of the number.
    >>>
    >>> <pre>
    >>> random_number = random.sample([0,1,2,3,4,5,6,7,8,9], 5) # choose 5
    >>> elements
    >>> code = 'this is a string' + str(random_number[0]) +
    >>> str(random_number[1]) + str(random_number[2]) + str(random_number[3])
    >>> + str(random_number[4])
    >>> </pre>
    >>>

    >>
    >> '%05i'%random.randint(0,99999)
    >> --
    >> http://mail.python.org/mailman/listinfo/python-list
    >>

    > This produces numbers other than 5 digit numbers. making the start
    > number 10000 should be fine.
    > --
    > http://mail.python.org/mailman/listinfo/python-list

    nevermind. my brain is tired tonight. this is the best solution.
     
    Gary M. Josack, Sep 28, 2008
    #7
  8. sotirac

    Mensanator Guest

    On Sep 28, 3:11�pm, "Gary M. Josack" <> wrote:
    > Chris Rebert wrote:
    > > On Sun, Sep 28, 2008 at 12:59 PM, sotirac <> wrote:

    >
    > >> Wondering if there is a better way to generate string of numbers with
    > >> a length of 5 which also can have a 0 in the front of the number.

    >
    > >> <pre>
    > >> �random_number = random.sample([0,1,2,3,4,5,6,7,8,9], 5) # choose 5
    > >> elements
    > >> �code = 'this is a string' + str(random_number[0]) +
    > >> str(random_number[1]) + str(random_number[2]) + str(random_number[3])
    > >> + str(random_number[4])

    >
    > > code = ''.join(str(digit) for digit in random_number)

    >
    > > Regards,
    > > Chris

    >
    > >> </pre>

    >
    > >> --
    > >>http://mail.python.org/mailman/listinfo/python-list

    >
    > will random.randint(10000,99999) work for you?


    It doesn't meet the OP's requirement that the number
    can start with 0. Also, the method the OP asks about
    returns a list of unique numbers, so no number can
    be duplicated. He can get 02468 but not 13345.

    Now, IF it's ok to have an arbitrary number of leading
    0s, he can do this:

    >>> str(random.randint(0,99999)).zfill(5)

    '00089'
    >>> str(random.randint(0,99999)).zfill(5)

    '63782'
    >>> str(random.randint(0,99999)).zfill(5)

    '63613'
    >>> str(random.randint(0,99999)).zfill(5)

    '22315'
     
    Mensanator, Sep 28, 2008
    #8
  9. On Sep 28, 3:44 pm, Mensanator <> wrote:
    > On Sep 28, 3:11 pm, "Gary M. Josack" <> wrote:
    >
    >
    >
    > > Chris Rebert wrote:
    > > > On Sun, Sep 28, 2008 at 12:59 PM, sotirac <> wrote:

    >
    > > >> Wondering if there is a better way to generate string of numbers with
    > > >> a length of 5 which also can have a 0 in the front of the number.

    >
    > > >> <pre>
    > > >> random_number = random.sample([0,1,2,3,4,5,6,7,8,9], 5) # choose 5
    > > >> elements
    > > >> code = 'this is a string' + str(random_number[0]) +
    > > >> str(random_number[1]) + str(random_number[2]) + str(random_number[3])
    > > >> + str(random_number[4])

    >
    > > > code = ''.join(str(digit) for digit in random_number)

    >
    > > > Regards,
    > > > Chris

    >
    > > >> </pre>

    >
    > > >> --
    > > >>http://mail.python.org/mailman/listinfo/python-list

    >
    > > will random.randint(10000,99999) work for you?

    >
    > It doesn't meet the OP's requirement that the number
    > can start with 0. Also, the method the OP asks about
    > returns a list of unique numbers, so no number can
    > be duplicated. He can get 02468 but not 13345.
    >
    > Now, IF it's ok to have an arbitrary number of leading
    > 0s, he can do this:
    >
    > >>> str(random.randint(0,99999)).zfill(5)

    > '00089'
    > >>> str(random.randint(0,99999)).zfill(5)

    > '63782'
    > >>> str(random.randint(0,99999)).zfill(5)

    > '63613'
    > >>> str(random.randint(0,99999)).zfill(5)

    >
    > '22315'


    Is a while loop until there are 5 distinct digits best otherwise?

    while 1:
    a= '%05i'% random.randint( 0, 99999 )
    if len( set( a ) )== 5: break
     
    Aaron \Castironpi\ Brady, Sep 28, 2008
    #9
  10. sotirac

    Tim Chase Guest

    > Wondering if there is a better way to generate string of numbers with
    > a length of 5 which also can have a 0 in the front of the number.


    If you want to resample the same digit multiple times, either of these
    two will do:

    >>> from random import choice
    >>> ''.join(str(choice(range(10))) for _ in range(5))

    '06082'

    >>> from string import digits
    >>> ''.join(choice(digits) for _ in range(5))

    '09355'


    If you need to prevent the digits from being reused

    >>> d = list(digits)
    >>> random.shuffle(digit)
    >>> ''.join(d[:5])

    '03195'

    I suspect that the zfill responses don't have the property of equally
    distributed "randomness", as the first digit may more likely be a zero.
    The methods here should give equal probabilities for each choice in
    each place.

    -tkc
     
    Tim Chase, Sep 28, 2008
    #10
  11. Gary M. Josack wrote:
    > Aaron "Castironpi" Brady wrote:
    >> On Sep 28, 2:59 pm, sotirac <> wrote:
    >>
    >>> Wondering if there is a better way to generate string of numbers with
    >>> a length of 5 which also can have a 0 in the front of the number.
    >>>
    >>> <pre>
    >>> random_number = random.sample([0,1,2,3,4,5,6,7,8,9], 5) # choose 5
    >>> elements
    >>> code = 'this is a string' + str(random_number[0]) +
    >>> str(random_number[1]) + str(random_number[2]) + str(random_number[3])
    >>> + str(random_number[4])
    >>> </pre>
    >>>

    >>
    >> '%05i'%random.randint(0,99999)
    >> --
    >> http://mail.python.org/mailman/listinfo/python-list
    >>

    > This produces numbers other than 5 digit numbers. making the start
    > number 10000 should be fine.


    Why do you think it's wrong?

    >>> import random
    >>> '%05i'%random.randint(0,99999)

    '09449'
    >>>


    IMO it's exactly what was required.

    Ciao, Michael.
     
    Michael Ströder, Sep 28, 2008
    #11
  12. On Sep 28, 4:08 pm, Michael Ströder <> wrote:
    > Gary M. Josack wrote:
    > > Aaron "Castironpi" Brady wrote:
    > >> On Sep 28, 2:59 pm, sotirac <> wrote:

    >
    > >>> Wondering if there is a better way to generate string of numbers with
    > >>> a length of 5 which also can have a 0 in the front of the number.

    >
    > >>> <pre>
    > >>>  random_number = random.sample([0,1,2,3,4,5,6,7,8,9], 5) # choose 5
    > >>> elements
    > >>>  code = 'this is a string' + str(random_number[0]) +
    > >>> str(random_number[1]) + str(random_number[2]) + str(random_number[3])
    > >>> + str(random_number[4])
    > >>> </pre>

    >
    > >> '%05i'%random.randint(0,99999)
    > >> --
    > >>http://mail.python.org/mailman/listinfo/python-list

    >
    > > This produces numbers other than 5 digit numbers. making the start
    > > number 10000 should be fine.

    >
    > Why do you think it's wrong?
    >
    >
    >
    > >>> import random
    > >>> '%05i'%random.randint(0,99999)

    > '09449'
    >
    > IMO it's exactly what was required.
    >
    > Ciao, Michael.


    As you read, there isn't agreement on whether the OP wanted
    replacement. His original code didn't; his spec seemed to.
     
    Aaron \Castironpi\ Brady, Sep 28, 2008
    #12
  13. sotirac

    Mensanator Guest

    On Sep 28, 3:54�pm, "Aaron \"Castironpi\" Brady"
    <> wrote:
    > On Sep 28, 3:44�pm, Mensanator <> wrote:
    >
    >
    >
    >
    >
    > > On Sep 28, 3:11 pm, "Gary M. Josack" <> wrote:

    >
    > > > Chris Rebert wrote:
    > > > > On Sun, Sep 28, 2008 at 12:59 PM, sotirac <> wrote:

    >
    > > > >> Wondering if there is a better way to generate string of numbers with
    > > > >> a length of 5 which also can have a 0 in the front of the number.

    >
    > > > >> <pre>
    > > > >> random_number = random.sample([0,1,2,3,4,5,6,7,8,9], 5) # choose 5
    > > > >> elements
    > > > >> code = 'this is a string' + str(random_number[0]) +
    > > > >> str(random_number[1]) + str(random_number[2]) + str(random_number[3])
    > > > >> + str(random_number[4])

    >
    > > > > code = ''.join(str(digit) for digit in random_number)

    >
    > > > > Regards,
    > > > > Chris

    >
    > > > >> </pre>

    >
    > > > >> --
    > > > >>http://mail.python.org/mailman/listinfo/python-list

    >
    > > > will random.randint(10000,99999) work for you?

    >
    > > It doesn't meet the OP's requirement that the number
    > > can start with 0. Also, the method the OP asks about
    > > returns a list of unique numbers, so no number can
    > > be duplicated. He can get 02468 but not 13345.

    >
    > > Now, IF it's ok to have an arbitrary number of leading
    > > 0s, he can do this:

    >
    > > >>> str(random.randint(0,99999)).zfill(5)

    > > '00089'
    > > >>> str(random.randint(0,99999)).zfill(5)

    > > '63782'
    > > >>> str(random.randint(0,99999)).zfill(5)

    > > '63613'
    > > >>> str(random.randint(0,99999)).zfill(5)

    >
    > > '22315'

    >
    > Is a while loop until there are 5 distinct digits best otherwise?


    Of course not.

    >
    > while 1:
    > � a= '%05i'% random.randint( 0, 99999 )
    > � if len( set( a ) )== 5: break


    How is this better than the OP's original code?
     
    Mensanator, Sep 28, 2008
    #13
  14. sotirac

    Mensanator Guest

    On Sep 28, 4:02�pm, Tim Chase <> wrote:
    > > Wondering if there is a better way to generate string of numbers with
    > > a length of 5 which also can have a 0 in the front of the number.

    >
    > If you want to resample the same digit multiple times, either of these
    > two will do:
    >
    > �>>> from random import choice
    > �>>> ''.join(str(choice(range(10))) for _ in range(5))
    > '06082'
    >
    > �>>> from string import digits
    > �>>> ''.join(choice(digits) for _ in range(5))
    > '09355'
    >
    > If you need to prevent the digits from being reused
    >
    > �>>> d = list(digits)
    > �>>> random.shuffle(digit)
    > �>>> ''.join(d[:5])
    > '03195'
    >
    > I suspect that the zfill responses don't have the property of equally
    > distributed "randomness", as the first digit may more likely be a zero.
    > � The methods here should give equal probabilities for each choice in
    > each place.


    Does he want equal probabilities of each digit in each place?

    >
    > -tkc
     
    Mensanator, Sep 28, 2008
    #14
  15. sotirac

    Guest

    Tim Chase:
    > I suspect that the zfill responses don't have the property of equally
    > distributed "randomness", as the first digit may more likely be a zero.


    This code I have shown before:
    str(randrange(100000)).zfill(5)

    If the numbers are equally distributed in [0, 99999], then the leading
    zeros have the correct distribution.

    A little test program for you:

    from string import digits
    from random import choice, randrange
    from collections import defaultdict

    def main():
    N = 1000000

    freqs1 = defaultdict(int)
    for i in xrange(N):
    n = "".join(choice(digits) for d in xrange(5))
    freqs1[n[0]] += 1
    print [freqs1[str(i)] for i in xrange(10)]

    freqs2 = defaultdict(int)
    for i in xrange(N):
    n = str(randrange(100000)).zfill(5)
    freqs2[n[0]] += 1
    print [freqs2[str(i)] for i in xrange(10)]

    import psyco; psyco.full()
    main()

    The output:
    [100153, 99561, 99683, 100297, 99938, 100162, 99738, 100379, 100398,
    99691]
    [99734, 100153, 100091, 100683, 99580, 99676, 99671, 100131, 100102,
    100179]

    Of course with a bit of math you can also demonstrate it :)

    Bye,
    bearophile
     
    , Sep 28, 2008
    #15
  16. sotirac

    dusans Guest

    On Sep 29, 12:06 am, Mensanator <> wrote:
    > On Sep 28, 3:54 pm, "Aaron \"Castironpi\" Brady"
    >
    >
    >
    >
    >
    > <> wrote:
    > > On Sep 28, 3:44 pm, Mensanator <> wrote:

    >
    > > > On Sep 28, 3:11 pm, "Gary M. Josack" <> wrote:

    >
    > > > > Chris Rebert wrote:
    > > > > > On Sun, Sep 28, 2008 at 12:59 PM, sotirac <> wrote:

    >
    > > > > >> Wondering if there is a better way to generate string of numbers with
    > > > > >> a length of 5 which also can have a 0 in the front of the number..

    >
    > > > > >> <pre>
    > > > > >> random_number = random.sample([0,1,2,3,4,5,6,7,8,9], 5) # choose 5
    > > > > >> elements
    > > > > >> code = 'this is a string' + str(random_number[0]) +
    > > > > >> str(random_number[1]) + str(random_number[2]) + str(random_number[3])
    > > > > >> + str(random_number[4])

    >
    > > > > > code = ''.join(str(digit) for digit in random_number)

    >
    > > > > > Regards,
    > > > > > Chris

    >
    > > > > >> </pre>

    >
    > > > > >> --
    > > > > >>http://mail.python.org/mailman/listinfo/python-list

    >
    > > > > will random.randint(10000,99999) work for you?

    >
    > > > It doesn't meet the OP's requirement that the number
    > > > can start with 0. Also, the method the OP asks about
    > > > returns a list of unique numbers, so no number can
    > > > be duplicated. He can get 02468 but not 13345.

    >
    > > > Now, IF it's ok to have an arbitrary number of leading
    > > > 0s, he can do this:

    >
    > > > >>> str(random.randint(0,99999)).zfill(5)
    > > > '00089'
    > > > >>> str(random.randint(0,99999)).zfill(5)
    > > > '63782'
    > > > >>> str(random.randint(0,99999)).zfill(5)
    > > > '63613'
    > > > >>> str(random.randint(0,99999)).zfill(5)

    >
    > > > '22315'

    >
    > > Is a while loop until there are 5 distinct digits best otherwise?

    >
    > Of course not.
    >
    >
    >
    > > while 1:
    > > a= '%05i'% random.randint( 0, 99999 )
    > > if len( set( a ) )== 5: break

    >
    > How is this better than the OP's original code?


    Wow didnt know about '%05i' :)
     
    dusans, Sep 29, 2008
    #16
  17. sotirac

    sotirac Guest

    On Sep 28, 5:22 pm, "Aaron \"Castironpi\" Brady"
    <> wrote:
    > On Sep 28, 4:08 pm, Michael Ströder <> wrote:
    >
    >
    >
    > > Gary M. Josack wrote:
    > > > Aaron "Castironpi" Brady wrote:
    > > >> On Sep 28, 2:59 pm, sotirac <> wrote:

    >
    > > >>> Wondering if there is a better way to generate string of numbers with
    > > >>> a length of 5 which also can have a 0 in the front of the number.

    >
    > > >>> <pre>
    > > >>>  random_number = random.sample([0,1,2,3,4,5,6,7,8,9], 5) # choose 5
    > > >>> elements
    > > >>>  code = 'this is a string' + str(random_number[0]) +
    > > >>> str(random_number[1]) + str(random_number[2]) + str(random_number[3])
    > > >>> + str(random_number[4])
    > > >>> </pre>

    >
    > > >> '%05i'%random.randint(0,99999)
    > > >> --
    > > >>http://mail.python.org/mailman/listinfo/python-list

    >
    > > > This produces numbers other than 5 digit numbers. making the start
    > > > number 10000 should be fine.

    >
    > > Why do you think it's wrong?

    >
    > > >>> import random
    > > >>> '%05i'%random.randint(0,99999)

    > > '09449'

    >
    > > IMO it's exactly what was required.

    >
    > > Ciao, Michael.

    >
    > As you read, there isn't agreement on whether the OP wanted
    > replacement.  His original code didn't; his spec seemed to.


    My value of the result string can be '00000' to '99999'.
     
    sotirac, Sep 29, 2008
    #17
    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. Girish Sahani
    Replies:
    17
    Views:
    577
    Boris Borcic
    Jun 9, 2006
  2. globalrev
    Replies:
    4
    Views:
    778
    Gabriel Genellina
    Apr 20, 2008
  3. Kevin
    Replies:
    7
    Views:
    184
    Kevin
    May 17, 2011
  4. Csaba  Gabor

    Length of longest contiguous digits exercise

    Csaba Gabor, Oct 23, 2009, in forum: Javascript
    Replies:
    19
    Views:
    186
  5. VK
    Replies:
    15
    Views:
    1,190
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page