On Sep 28, 3:54�pm, "Aaron \"Castironpi\" Brady" <[EMAIL PROTECTED]> wrote: > On Sep 28, 3:44�pm, Mensanator <[EMAIL PROTECTED]> wrote: > > > > > > > On Sep 28, 3:11 pm, "Gary M. Josack" <[EMAIL PROTECTED]> wrote: > > > > Chris Rebert wrote: > > > > On Sun, Sep 28, 2008 at 12:59 PM, sotirac <[EMAIL PROTECTED]> 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? -- http://mail.python.org/mailman/listinfo/python-list