generating promotion code using python

Hellnar picture Hellnar · Jan 21, 2010 · Viewed 7.5k times · Source

By using python language, what would be a clever / efficient way of generating promotion codes. Like to be used for generating special numbers for discount coupons. like: 1027828-1

Thanks

Answer

GreenMatt picture GreenMatt · Jan 21, 2010

The following isn't particularly pythonic or particularly efficient, but it might suffice:

 import random
 def get_promo_code(num_chars):
     code_chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
     code = ''
     for i in range(0, num_chars):
         slice_start = random.randint(0, len(code_chars) - 1)
         code += code_chars[slice_start: slice_start + 1]
     return code