[Tutor] Generate 8 digit random number

Pietro Ciuffo pietro.ciuffo at nekhem.com
Fri Aug 26 16:09:08 CEST 2005


On Fri, Aug 26, 2005 at 01:50:04PM +0000, Alberto Troiano 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

Hi,
The following code seeems to work.

from random import choice
lnd='0123456789'
print ''.join(map(lambda x,y=lnd: choice(y), range(8)))

Bye

-- 
The Old Man and the Sea LITE(tm) -- by Ernest Hemingway
'An old man goes fishing, but doesn't have much luck.'


More information about the Tutor mailing list