[Tutor] Generate 8 digit random number

Ertl, John john.ertl at fnmoc.navy.mil
Fri Aug 26 23:26:27 CEST 2005


Sorry for that you will have to change the %09 to a %08 to get 8 digits.   I
got a bit to fixated on the 99999999

John Ertl  

 -----Original Message-----
From: 	Ertl, John  
Sent:	Friday, August 26, 2005 2:23 PM
To:	Alberto Troiano; tutor at python.org
Subject:	RE: [Tutor] Generate 8 digit random number

Alberto

If you don't mind having leading 0 then you could just do the random like
you did then format it to 9 digits.

You could give this a try

num = random.randrange(00000000,99999999)
num8 = "%09i" % num

John Ertl 


 -----Original Message-----
From: 	Byron [mailto:byron at christianfreebies.com] 
Sent:	Friday, August 26, 2005 1:50 PM
To:	Alberto Troiano; tutor at python.org
Subject:	Re: [Tutor] Generate 8 digit random number

Hi Alberto,

Here's how to do it:

-------------------------------

import random

def generateKey():
	nums = "0123456789"
	strNumber = ""
	count = 0
	while (count < 8):
		strNumber += nums[random.randrange(len(nums))]
		count += 1
	print strNumber
	
# A quick test...
count = 0
while (count < 10000):
	generateKey()
	count += 1


-------------------------------

Byron  :-)

---


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
> 
> Alberto
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list