Click here to Skip to main content
15,880,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();


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)
Posted
Comments
Kornfeld Eliyahu Peter 18-May-15 3:47am    
Using what? Java or JavaScript?
sigmaxbpo 18-May-15 4:12am    
i need it in java..
Kornfeld Eliyahu Peter 18-May-15 4:20am    
Remember that Java and JavaScript are two different things altogether!!!
sigmaxbpo 18-May-15 4:26am    
ya bro, i understand java and javascript are different code. Sorry i put wrong tag
Kornfeld Eliyahu Peter 18-May-15 4:20am    
You may use this: https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/RandomStringUtils.html

1 solution

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.
 
Share this answer
 
Comments
sigmaxbpo 18-May-15 4:38am    
Hi Thank you Andy Lanng, i understand that for UUID method we can never ever use them and them down a certain length to use them as ID. For the method u mentioned in 1 / 2 could you provide some code sample ?
Andy Lanng 18-May-15 4:44am    
The solution mentions concepts. For the first option, I suggest using the link Kornfeld Eliyahu Peter gave you. It looks like a great shortcut for creating random codes. Google the method names in that link to find examples.
As for the 2nd option, I would advise against using it unless absolutely necessary. For one thing, publishing any method on a public forum like this will open up the algorithm for hacking.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900