Re: tweaking random number
Nikhil Verma wrote: > Hi All > > I want to generate a random number of 8 digits which involve 3 number and > 5 digits. > Like this :- > > def random_number(): > # do something > > random_number() > "123abcde" # first 3 numbers and 5 letters after the numbers. > > I am able to generate the random number 8 digit like this:- > > def random_number(): > characters = list(string.ascii_lowercase + string.ascii_uppercase\ > + string.digits) > coll_rand = [] > for i in range(8): > random.shuffle(characters) > coll_rand.append(characters[0]) > return ''.join(coll_rand) > > This generates like this "Kkrgt56r" > > Thanks in advance If you generalize your random_number() function >>> import random, string >>> def random_code(n=8, chars=string.ascii_lowercase+string.ascii_uppercase+string.digits): ... return "".join(random.choice(chars) for _ in range(n)) ... >>> random_code() 'NgcLhYdR' >>> random_code() 'j9gafcHh' >>> random_code(chars="123ABC") 'C311BA31' >>> random_code(n=4) 'MAsV' you can use it as a building block quite easily: >>> def three_five(): ... return random_code(3, string.digits) + random_code(5, string.ascii_lowercase + string.ascii_uppercase) ... >>> three_five() '656xEWmd' >>> three_five() '589XqZcI' >>> three_five() '168iOOIM' -- http://mail.python.org/mailman/listinfo/python-list
Re: tweaking random number
On Wed, May 9, 2012 at 5:01 PM, Nikhil Verma wrote: > Hi All > > I want to generate a random number of 8 digits which involve 3 number and 5 > digits. (That's 3 digits and 5 letters) Pretty easy. Do you want to distinguish between uppercase and lowercase letters? Your current random_number function (btw, I wouldn't call it "number" as it isn't one) is most of one possible solution. Divide it into two parts, one part that generates the digits and another part that generates the letters. Your 'characters' template would thus be different for the two parts. There are other solutions, which involve the generation of less random numbers, but your way will work. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: tweaking random number
On 09/05/2012 08:01, Nikhil Verma wrote: Hi All I want to generate a random number of 8 digits which involve 3 number and 5 digits. Like this :- def random_number(): # do something random_number() "123abcde" # first 3 numbers and 5 letters after the numbers. I am able to generate the random number 8 digit like this:- def random_number(): characters = list(string.ascii_lowercase + string.ascii_uppercase\ + string.digits) coll_rand = [] for i in range(8): random.shuffle(characters) coll_rand.append(characters[0]) return ''.join(coll_rand) This generates like this "Kkrgt56r" Use random.choice to pick a random digit or a random letter. -- http://mail.python.org/mailman/listinfo/python-list
tweaking random number
Hi All I want to generate a random number of 8 digits which involve 3 number and 5 digits. Like this :- def random_number(): # do something random_number() "123abcde" # first 3 numbers and 5 letters after the numbers. I am able to generate the random number 8 digit like this:- def random_number(): characters = list(string.ascii_lowercase + string.ascii_uppercase\ + string.digits) coll_rand = [] for i in range(8): random.shuffle(characters) coll_rand.append(characters[0]) return ''.join(coll_rand) This generates like this "Kkrgt56r" Thanks in advance -- Regards Nikhil Verma +91-958-273-3156 -- http://mail.python.org/mailman/listinfo/python-list