[Tutor] Generate 8 digit random number

Terry Carroll carroll at tjc.com
Sat Aug 27 00:06:05 CEST 2005

On Fri, 26 Aug 2005, Alberto Troiano wrote:

> I need to generate a password..It has to be an 8 digit number and it has
> to be random
> import random
> random.randrange(00000000,99999999)
> The code works but sometimes it picks a number with 7 digits. Is there any 
> way that I can tell him to select always a random number with 8 digits?

well, you've gotten a lot of good answers, so let me chime in with a 
really ugly one:

>>> ''.join(['0000000',str(random.randrange(00000000,99999999))])[-8:]

(I don't really recommend this, but this is pretty much akin to the
prefered way in IBM's EXEC2 language as I used to use it some 25 years

More information about the Tutor mailing list