Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Generate random registration codes

Posted on 2003-03-14
11
Medium Priority
?
379 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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Technology Partners: 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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month11 days, 4 hours left to enroll

572 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