[Tutor] Generate 8 digit random number

Byron byron at christianfreebies.com
Fri Aug 26 22:49:41 CEST 2005


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
> 




More information about the Tutor mailing list