Generating unique, hard-to-guess “coupon” codes

前端 未结 8 772
耶瑟儿~
耶瑟儿~ 2020-12-23 10:24

My Rails app needs to generate electronic coupons for users. Each coupon given should have a unique coupon code that can be redeemed on our system.

For example a cou

相关标签:
8条回答
  • 2020-12-23 11:21

    Get an epoch timestamp and base encode it.. as long as you save a record of it somewhere you can compare if it's valid when it's used.

    If you need it to be enterable by hand you can always .string() it down to first 8 or so characters.

    0 讨论(0)
  • 2020-12-23 11:27

    Draw random numbers using a proven generator (http://en.wikipedia.org/wiki/List_of_pseudorandom_number_generators).

    Assume you deliver 333 coupons per day and these are valid for 30 days. So you have to store 10000 numbers and ensure that a forger cannot find one by chance.

    If your numbers have 10 significant digits (~32 bits, ~8 hex digits), the probability of such an event is one over a million. Of course you can use more.

    0 讨论(0)
提交回复
热议问题