tweaking random number

MRAB python at mrabarnett.plus.com
Wed May 9 03:38:34 EDT 2012


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.



More information about the Python-list mailing list