generate random digits with length of 5

Aaron "Castironpi" Brady castironpi at gmail.com
Sun Sep 28 17:22:08 EDT 2008


On Sep 28, 4:08 pm, Michael Ströder <mich... at stroeder.com> wrote:
> Gary M. Josack wrote:
> > Aaron "Castironpi" Brady wrote:
> >> On Sep 28, 2:59 pm, sotirac <soti... at gmail.com> 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.



More information about the Python-list mailing list