Re: Generating random alphanumeric codes
Dave Angel <d <at> davea.name>
2012-06-26 20:47:21 GMT
On 06/26/2012 03:47 PM, Martin A. Brown wrote:
> : Would anyone have tips on how to generate random 4-digit
> : alphanumeric codes in python? Also, how does one calculate the
> : number of possible combinations?
> Here are some thoughts that come to my mind:
> import string
> import random
> # -- technique #1, using random.choice
> print ''.join([random.choice(string.lowercase) for x in range(4)])
> # -- technique #2, using random.shuffle
> t = [ x for x in string.lowercase ]
> # -- technique #3, using random.sample
> I would be quite surprised if there were not more efficient ways of
> accomplishing this.
Two problems that I see:
string.lowercase includes the lowercase letters, but not the digits.
Your methods 2 and 3 produce different results than method1. The OP
never specified which he wanted, but the distinction can well be important.
Likewise his query about how many possibilities there are depends on the
Are duplicates permitted between the four characters in any given
Tutor maillist - Tutor <at> python.org
To unsubscribe or change subscription options: