| |
 Search this list this category for: (Advanced)

Mailing List Archive: Python: Python

# tweaking random number

varma.nikhil22 at gmail

May 9, 2012, 12:01 AM

Post #1 of 4 (201 views)
 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"

--
Regards
Nikhil Verma
+91-958-273-3156

python at mrabarnett

May 9, 2012, 12:38 AM

Post #2 of 4 (196 views)
 Re: tweaking random number [In reply to]
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

rosuav at gmail

May 9, 2012, 12:44 AM

Post #3 of 4 (194 views)
 Re: tweaking random number [In reply to]
On Wed, May 9, 2012 at 5:01 PM, Nikhil Verma <varma.nikhil22 [at] gmail> 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

__peter__ at web

May 9, 2012, 1:30 AM

Post #4 of 4 (192 views)
 Re: tweaking random number [In reply to]
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"
>

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