26 Jun 2012 16:52

## Generating random alphanumeric codes

HI,

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?

--
Regards,
Sithembewena Lloyd Dube
```_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
```
26 Jun 2012 17:04

### Re: Generating random alphanumeric codes

On Tue, Jun 26, 2012 at 4:52 PM, Sithembewena Lloyd Dube wrote:
HI,

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?

Python's, random module is your friend. I'd suggest looking at the random.choice() function. As for the number of possibilities, if your codes are not case sensitive, you'll have 10 + 26 = 36 possibilities for each character/digit. 4 digits, so 36**4 possible codes.

Hugo

```_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
```
26 Jun 2012 17:08

### Re: Generating random alphanumeric codes

First of all, determine your alphabet (the pool of characters you'll derive your 4-character code from):

- For example, using an English alphabet with both lowercase and uppercase letters and digits 0-9 makes for 62 characters (26 + 26 + 10).

Then, ask if you want to allow a character to repeat itself in the 4-character code.

- If so, then it's easy: take the length of your alphabet, and calculate (length)^4. In my example above, it would be 62^4 = 14,776,336

- If not, then it's a little more complicated, calculate (length) * (length-1) * (length-2) * (length-3), or in other words the factorial of length minus the factorial of (length - 4). In my example, 62! - (62-4)! = 13,388,280

AR

On Tue, Jun 26, 2012 at 10:52 AM, Sithembewena Lloyd Dube wrote:
HI,

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?

--
Regards,
Sithembewena Lloyd Dube

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

```_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
```
26 Jun 2012 17:09

### Re: Generating random alphanumeric codes

Correcting what was said below.

- If not, then it's a little more complicated, calculate (length) * (length-1) * (length-2) * (length-3), or in other words the factorial of length divided by the factorial of (length - 4). In my example, 62! / (62-4)! = 13,388,280

On Tue, Jun 26, 2012 at 11:08 AM, taserian wrote:
First of all, determine your alphabet (the pool of characters you'll derive your 4-character code from):

- For example, using an English alphabet with both lowercase and uppercase letters and digits 0-9 makes for 62 characters (26 + 26 + 10).

Then, ask if you want to allow a character to repeat itself in the 4-character code.

- If so, then it's easy: take the length of your alphabet, and calculate (length)^4. In my example above, it would be 62^4 = 14,776,336

- If not, then it's a little more complicated, calculate (length) * (length-1) * (length-2) * (length-3), or in other words the factorial of length minus the factorial of (length - 4). In my example, 62! - (62-4)! = 13,388,280

AR

On Tue, Jun 26, 2012 at 10:52 AM, Sithembewena Lloyd Dube wrote:
HI,

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?

--
Regards,
Sithembewena Lloyd Dube

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

```_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
```
26 Jun 2012 21:47

### Re: Generating random alphanumeric codes

```
Hello,

: 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 ]
random.shuffle(t)
''.join(t[0:4])

# -- technique #3, using random.sample
''.join(random.sample(string.lowercase,4))

I would be quite surprised if there were not more efficient ways of
accomplishing this.

-Martin

--

--
Martin A. Brown
http://linux-ip.net/
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

```
26 Jun 2012 22:47

### Re: Generating random alphanumeric codes

```On 06/26/2012 03:47 PM, Martin A. Brown wrote:
> Hello,
>
>  : 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 ]
>   random.shuffle(t)
>   ''.join(t[0:4])
>
>   # -- technique #3, using random.sample
>   ''.join(random.sample(string.lowercase,4))
>
> I would be quite surprised if there were not more efficient ways of
> accomplishing this.
>
> -Martin
>
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
same question:
Are duplicates permitted between the four characters in any given

--

--

DaveA

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

```
27 Jun 2012 16:05

### Re: Generating random alphanumeric codes

I've always liked uuid4

>>> from uuid import uuid4
>>> str(uuid4())[:4]
'0ca6'

On Tue, Jun 26, 2012 at 3:47 PM, Dave Angel wrote:
On 06/26/2012 03:47 PM, Martin A. Brown wrote:
> Hello,
>
>  : 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 ]
>   random.shuffle(t)
>   ''.join(t[0:4])
>
>   # -- technique #3, using random.sample
>   ''.join(random.sample(string.lowercase,4))
>
> I would be quite surprised if there were not more efficient ways of
> accomplishing this.
>
> -Martin
>
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
same question:
Are duplicates permitted between the four characters in any given

--

DaveA

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

```_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
```

Gmane