Alpha Numeric Unique Random Number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vrkamalakar
    New Member
    • Jan 2007
    • 16

    #1

    Alpha Numeric Unique Random Number

    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.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by vrkamalakar
    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?

    Comment

    • vrkamalakar
      New Member
      • Jan 2007
      • 16

      #3
      Can you give me a small example.......i e., piece of code

      Thanks & Regards,
      Kamal


      Originally posted by r035198x
      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?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by vrkamalakar
        Can you give me a small example.......i e., 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

        Code:
        public class Test {
        	public static void main(String... args) {
        		char[] chars = new char[36];
        		int i = 0;
        		//This is for lower case characters only
        		for(char c = 'a'; c <= 'z';c++) {
        			chars[i++] = c;
        		}
        		i = 26;
        		for(char c = '0'; c <= '9';c++) {
        			chars[i++] = c;
        		}
        		int numberOfCodes = 0;
        		while (numberOfCodes < 10) {
        			int numChars = 10 + (int)(Math.random()*5.5);
        			String code = "";
        			for(i = 0; i < numChars; i++) {
        				char c = chars[(int)(Math.random() * chars.length)];
        				code = code + c;
        			}
        			System.out.println("Code is :"+code);
         			System.out.println("Length of code is :"+code.length());
         			System.out.println("***********************************");
        			numberOfCodes++;
        		}
        	}
        }

        Comment

        • vrkamalakar
          New Member
          • Jan 2007
          • 16

          #5
          Thanks a lot :)

          Regards,
          Kamal


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

          Code:
          public class Test {
          	public static void main(String... args) {
          		char[] chars = new char[36];
          		int i = 0;
          		//This is for lower case characters only
          		for(char c = 'a'; c <= 'z';c++) {
          			chars[i++] = c;
          		}
          		i = 26;
          		for(char c = '0'; c <= '9';c++) {
          			chars[i++] = c;
          		}
          		int numberOfCodes = 0;
          		while (numberOfCodes < 10) {
          			int numChars = 10 + (int)(Math.random()*5.5);
          			String code = "";
          			for(i = 0; i < numChars; i++) {
          				char c = chars[(int)(Math.random() * chars.length)];
          				code = code + c;
          			}
          			System.out.println("Code is :"+code);
           			System.out.println("Length of code is :"+code.length());
           			System.out.println("***********************************");
          			numberOfCodes++;
          		}
          	}
          }

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by vrkamalakar
            Thanks a lot :)

            Regards,
            Kamal
            Just remember to include your own code when asking next time.

            Comment

            Working...