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@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.