Coupon code generation

后端 未结 5 841
悲&欢浪女
悲&欢浪女 2020-12-07 08:24

I would like to generate coupon codes , e.g. AYB4ZZ2. However, I would also like to be able to mark the used coupons and limit their global number, let\'s say <

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-07 09:11

    You can use a base-36 number system. Assume that you want 6 characters in the coupen output.

    pseudo code for MakeCoupon

    MakeCoupon(n) {

    Have an byte array of fixed size, say 6. Initialize all the values to 0. convert the number to base - 36 and store the 'digits' in an array (using integer division and mod operations) Now, for each 'digit' find the corresponding ascii code assuming the digits to start from 0..9,A..Z With this convension output six digits as a string.

    }

    Now the calculating the number back is the reverse of this operation.

    This would work with very large numbers (35^6) with 6 allowed characters.

提交回复
热议问题