Sithembewena Lloyd Dube | 26 Jun 2012 16:53
Picon

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?

Thanks in advance.

--
Regards,
Sithembewena Lloyd Dube

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users <at> googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Adrian Bool | 26 Jun 2012 17:13
Picon

Re: Generating random alphanumeric codes


On 26 Jun 2012, at 15:53, Sithembewena Lloyd Dube 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?

For upper case, lower case and digits we have,

26 + 26 + 10 = 62 
...possibilities for each position.  If you have four positions you have,

62 * 62 * 62 * 62 = 14776336
... possible combinations.

You can create a random number in this range by,

import Random from random
def build_population(spans):
for span in spans:
for x in range(ord(span[0]), ord(span[1]) + 1):
yield chr(x)

population = list(build_population( (('A','Z'), ('a', 'z'), ('0', '9')) ))
r = Random()
r.sample(population, 4)
['C', 'l', 'q', 'y']
You may need to look into getting some entropy into the random number generator...

Cheers

aid

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users <at> googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Patricio Valarezo | 26 Jun 2012 19:28
Picon
Gravatar

Re: Generating random alphanumeric codes

El 26/06/12 10:13, Adrian Bool escribió:

On 26 Jun 2012, at 15:53, Sithembewena Lloyd Dube 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?

mm... if I undertand you, maybe this could be useful:

http://docs.python.org/library/itertools.html

specially the combinations

http://docs.python.org/library/itertools.html#itertools.combinations

PV
-- patoVala Linux User#280504 Hablando en http://www.elprimoalcahuete.com "The difference between a good haircut and a bad one is seven days." +--[ DSA 1024]----+ | .o.. | | .oo.. | | . =... . | | . = + ... o . | | * .S= + o | | . . o = E | | . o | | | | | +-----------------+

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users <at> googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Gmane