[Tutor] Generate 8 digit random number

Alan Gauld ukc802591034 at btconnect.com
Sat Aug 27 00:02:59 CEST 2005

> I need to generate a password..It has to be an 8 digit number and it
> has to be random

I assume you mean a string representing an 8 digit random number?
If so...

> 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?

Eventually it will generate a single digit number if its truly random!

But when you convert it to a string (see assumption above!) you can
pad it with zeros

passString = "%08d" % random.randrange(0,99999999)

Is that what you want?

Alan G.

More information about the Tutor mailing list