[Tutor] Generate 8 digit random number

Michael P. Reilly arcege at gmail.com
Sat Aug 27 01:52:04 CEST 2005


On 8/26/05, Alberto Troiano <albertito_g at hotmail.com> wrote:
> 
> Hi everyone
> 
> I need to generate a password..It has to be an 8 digit number and it has 
> to
> be random
> 
> The code I've been trying is the following:
> 
> 
> 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?
> 
> Thanks in advanced
> 
> Alberto
> 

Along with everyone else's solutions, I'll throw out:

str(random.randrange(99999999)).zfill(8)

-Arcege
-- 
There's so many different worlds,
So many different suns.
And we have just one world,
But we live in different ones.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050826/7d169729/attachment.html


More information about the Tutor mailing list