?
Solved

Generate random registration codes

Posted on 2003-03-14
11
Medium Priority
?
378 Views
Last Modified: 2010-03-31
I want to create registraion codes randomly.
How can I generate such a registration code containing numbers and characters. An example of how the registration code should look like is the registraion code when installing windows: AX54G-55BZD-IJQW9-BZU41 (or sth. like that).

The code will be stored in a database und must be unique.

Thank you in advance.

Dirk
Berlin, Germany
0
Comment
Question by:dirku
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8135449
long guid = new java.util.Random().nextLong()
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8135474
0
 

Author Comment

by:dirku
ID: 8135481
It should be a pure Java version.
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:dirku
ID: 8135503
It should be a pure Java version.
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8137308
char[] allowed = {'A','B','1' ... } //The letters you allow in the reg number:

string getReg() {
  String reg = genFive + "-" + genFive() +"-" ...;
  return reg;
}

string genFive() {
  Random ran = new Random();
  int length = allowed.length();
  String code = "";
  for (int i = 0; i<5 ; i++) {
    code += allowed[ran.nextInt()%length];
  }
return code;

}
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8137314
You can change the genFive() for loop, so it just makes a 25character registeration code, without the '-' ...
0
 

Expert Comment

by:SpideyMod
ID: 8140502
A request for deletion has been made citing duplicate question.  Experts, if you disagree with a refund, object within the next 72 hours.

SpideyMod
Community Support Moderator @Experts Exchange
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8142317
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8142534
0
 

Expert Comment

by:SpideyMod
ID: 8143253
CEHJ,
Thanks! I agree with you.  This question will continue.  I will post in the other duplicate question.  Thanks.

dirku,
Your responsibility remains to provide feedback to the experts assisting you here.

SpideyMod
Community Support Moderator @Experts Exchange
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 400 total points
ID: 8146089
Use Math.random () (multiplied with some number and rounded) to generate the integers and for aplhabets, declare a string containing all the alphabets from A to Z and then generate another random number between 0 and 26, which will index this string to choose the alphabet. Before adding the entry to the database, check if its already there or not. If it is there, then generate another random combination in the same manner.

Hope that much helps! Please feel free to ask if you have more doubts.

Mayank.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question