如何生成6/8位的随机唯一优惠券号码 [英] how to generate randomly UNIQUE coupon number which are 6 / 8 digit

查看:857
本文介绍了如何生成6/8位的随机唯一优惠券号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨亲爱的专家们......我试图生成字母数字6到8位的优惠券号码,这些号码是唯一的,如果优惠券号码键不在列表中,则会出现错误信息。你可以指导我吗?



我正在尝试搜索类似的结果,但我看到了很多人使用



Hi dear experts..im trying to do generate Alphanumeric 6 to 8 digit coupon number which are UNIQUELY and if the coupon number key in is not in the list it will have error message. Can you please guide me ?

Im trying search for similiar result but i saw alot of ppl using

String uniqueID = UUID.randomUUID().toString();





但我不知道如何使用它,我试图导入相同的代码,但它不支持我当前的Java编译级别(jdk 1.5)



but i have no idea how to use it, i trying to import the same code but it not supported with my current java compilation level(jdk 1.5)

推荐答案

UUID是128位代码。这个想法是有很多可能的组合(3.4 * 10 ^ 36),每个随机组合都是唯一的。它不会是6-8位数。



优惠券代码不应该是连续的。这使得他们无法攻击。



您可以使用以下两种方法中的一种:



1 :存储所有以前的优惠券代码,以便生成的任何新优惠券可以排除它们

2:基于某些算法的生成,例如[基于日期]



我目前使用第一个选项,因为永远不会有这么多代码存储或查询它们将是一个问题。代码可以在生成的代码存在时循环。代码延迟为毫秒,优惠券代码仅为广告系列生成一次。



第二种技术非常适合生成大量代码。如果你为[打印优惠券]制作一个独特的代码,那么这就是使用的技术,但它非常复杂。
UUID is a 128 bit code. The idea is that there are so many possible combinations (3.4*10^36) that every random one will be unique. It will not be 6-8 digits.

Coupon codes should not be sequential. This makes them hack-able.

You can use 1 of a couple of methods:

1: Store all previous coupon codes so any new ones generated can exclude them
2: Base the generation of some algorithm which is [date based for example]

I currently use the first option as there will never be so many codes that storing or querying them will be an issue. The code can event just loop while the generated code exists. The delay in the code is milliseconds and coupon codes are only generated once for a campaign.

The second technique works great for generating a high volume of codes. If you're producing a unique code for [printed coupons for example] then this is the technique to use but it is quite complex.


这篇关于如何生成6/8位的随机唯一优惠券号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆