views:

146

answers:

3

Hello 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

+1  A: 

if you need a 6-digit # you could do this until you found a unique value:

import random
print str(random.randint(100000, 999999))

or go sequentially...

jspcal
899,999 is WAYYYYYYYYY TOOOOOOO SMALLLLLLLLLLLL!!!!!!!!!!!!!
Rook
no it's *exactly* what the OP requested. they *want* people to share, post, and easily type them in at checkout (see some examples: `SAVE10` or `5OFF25` or `TAKE2` or `EXTRA20` or `WORK15` at major brand name stores (http://couponalbum.com) - easily "crackable" by dictionary, but they're *not* passwords!) if your promo code needs to be cryptographically secure, it's *not* a promo code, it's a one-time use credit card token that wouldn't be distributed as a *promotion*.
jspcal
<showboating> Well I can write an exploit to break your weak systems. Oah and I just got the check mark. </showboating>
Rook
@unknown: i wouldn't brag about posting an incorrect answer. promo codes are *available online* to anyone who wants them by searching google. if you want to spend 2 days trying to find something anyone can find on google in 2 seconds, have a blast. and you still have to *buy* the product, the store is making a profit. amazon will love to take your money though
jspcal
I wish I could vote you down twice. I hope you get hacked so you can wake up.
Rook
+1 because you are just right (the OP clearly states: *generating special numbers for discount coupons*, and a discount coupon is normally not a one-time-thingy)
Felix Kling
Your code isn't python3 compatible
Rook
+2  A: 

1027828-1 is extremely small. An attacker can make a ~million guesses using only a couple lines of code and maybe a few days.

This is a good way to produce a hard to predict number using python, it works under linux and windows. It is base64'ed for binary safety, depending what you are doing with it you might want to urllib.urlencode() but I would avoid base10 because it doesn't store as much information.

import os
import base64

def secure_rand(len=8):
    token=os.urandom(len)
    return base64.b64encode(token)

print(secure_rand())

As a side note this is generating a full byte, which is base256. 256^8 is 18446744073709551616 which should be large enough.

Rook
base64 is *not suitable* for a promo code. it's case-sensitive, contains punctuation (which can break urls and forum posts), is long, and easy for customers to misspell. this means stores make less revenue. see couponcabin.com for an example of how to create a code.
jspcal
+1  A: 

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
GreenMatt