By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,032 Members | 811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,032 IT Pros & Developers. It's quick & easy.

Alpha Numeric Unique Random Number

P: 16
Hi,

Can I get a piece of code which can generate a 10 - 15 digit/character of uinque Random Number. The generated random number can contain both characters and digits.

Regards,
Kamalakar.
Jan 29 '07 #1
Share this Question
Share on Google+
5 Replies


10K+
P: 13,264
Hi,

Can I get a piece of code which can generate a 10 - 15 digit/character of uinque Random Number. The generated random number can contain both characters and digits.

Regards,
Kamalakar.
A simple way of doing it is to use a char array containing all the characters that you want to use (including the numbers) and then pick a character at random until you reach anywhere between 10-15 characters. Do you still need more help on it?
Jan 29 '07 #2

P: 16
Can you give me a small example.......ie., piece of code

Thanks & Regards,
Kamal


A simple way of doing it is to use a char array containing all the characters that you want to use (including the numbers) and then pick a character at random until you reach anywhere between 10-15 characters. Do you still need more help on it?
Jan 29 '07 #3

10K+
P: 13,264
Can you give me a small example.......ie., piece of code

Thanks & Regards,
Kamal
You are a very lucky guy. Rarely in this forum do you get code when you have posted none yourself

Expand|Select|Wrap|Line Numbers
  1. public class Test {
  2.     public static void main(String... args) {
  3.         char[] chars = new char[36];
  4.         int i = 0;
  5.         //This is for lower case characters only
  6.         for(char c = 'a'; c <= 'z';c++) {
  7.             chars[i++] = c;
  8.         }
  9.         i = 26;
  10.         for(char c = '0'; c <= '9';c++) {
  11.             chars[i++] = c;
  12.         }
  13.         int numberOfCodes = 0;
  14.         while (numberOfCodes < 10) {
  15.             int numChars = 10 + (int)(Math.random()*5.5);
  16.             String code = "";
  17.             for(i = 0; i < numChars; i++) {
  18.                 char c = chars[(int)(Math.random() * chars.length)];
  19.                 code = code + c;
  20.             }
  21.             System.out.println("Code is :"+code);
  22.              System.out.println("Length of code is :"+code.length());
  23.              System.out.println("***********************************");
  24.             numberOfCodes++;
  25.         }
  26.     }
  27. }
Jan 29 '07 #4

P: 16
Thanks a lot :)

Regards,
Kamal


You are a very lucky guy. Rarely in this forum do you get code when you have posted none yourself

Expand|Select|Wrap|Line Numbers
  1. public class Test {
  2.     public static void main(String... args) {
  3.         char[] chars = new char[36];
  4.         int i = 0;
  5.         //This is for lower case characters only
  6.         for(char c = 'a'; c <= 'z';c++) {
  7.             chars[i++] = c;
  8.         }
  9.         i = 26;
  10.         for(char c = '0'; c <= '9';c++) {
  11.             chars[i++] = c;
  12.         }
  13.         int numberOfCodes = 0;
  14.         while (numberOfCodes < 10) {
  15.             int numChars = 10 + (int)(Math.random()*5.5);
  16.             String code = "";
  17.             for(i = 0; i < numChars; i++) {
  18.                 char c = chars[(int)(Math.random() * chars.length)];
  19.                 code = code + c;
  20.             }
  21.             System.out.println("Code is :"+code);
  22.              System.out.println("Length of code is :"+code.length());
  23.              System.out.println("***********************************");
  24.             numberOfCodes++;
  25.         }
  26.     }
  27. }
Jan 29 '07 #5

10K+
P: 13,264
Thanks a lot :)

Regards,
Kamal
Just remember to include your own code when asking next time.
Jan 29 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.