Generating a random alphanumeric sequence

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dav3
    New Member
    • Nov 2006
    • 94

    Generating a random alphanumeric sequence

    Howdy folks, was wondering if anyone could assist me in a better method for generating a alphanumeric sequence of 6 characters/digits. The method I am currently using gives me a random license plate number for a project I am working on. But I am unsatisfied with the results I am getting. Here is my current method, it works but like I said there has to be a better way of doing it.

    Code:
    public static String plate()
    {
        int x = 6;
    	char[] plate = new char[x];
    	int c  = 'A';	    	
        for(int p = 0; p < 6; p++)
        {
        	int vehiclePlate = 0 + (int) (Math.random()* 6);
        	switch(vehiclePlate)
        	{
        		case 0: c = '0' +  (int)(Math.random() * 10); break;
        		case 1: c = 'A' +  (int)(Math.random() * 26); break;
        	}
        	plate[p] = (char)c;
        }
        return new String(plate);
    }
    Any help, suggestions or links to any relevant info is appreciated. Thanks.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Umm, the method you have looks perfectly fine, so why not use it?

    An alternative would be to create a character array holding the alphabet and numbers 0-9. Then you could build your string by adding random characters from the array.

    Comment

    • dav3
      New Member
      • Nov 2006
      • 94

      #3
      Originally posted by Ganon11
      Umm, the method you have looks perfectly fine, so why not use it?

      An alternative would be to create a character array holding the alphabet and numbers 0-9. Then you could build your string by adding random characters from the array.
      The results I am getting are:

      AAAAAW
      AAACCA
      AAAAAA
      AAAAXA

      ETC..... they all are not exactly like that. And once every 20 times or so I get results that are satisfactory to me. I will try your alternate method. TY.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Ahh...you hadn't mentioned the part where it wasn't working very well ;)

        The problem is, you generate random numbers between 0 and 5 to make a character, but you only test the cases 0 and 1. If you added the final 4 case statements, this would work perfectly well.

        Comment

        • dav3
          New Member
          • Nov 2006
          • 94

          #5
          Originally posted by Ganon11
          Ahh...you hadn't mentioned the part where it wasn't working very well ;)

          The problem is, you generate random numbers between 0 and 5 to make a character, but you only test the cases 0 and 1. If you added the final 4 case statements, this would work perfectly well.
          Wow that worked so well.

          *astonished*

          Ty Ganon.

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            No problems, glad to help!

            Comment

            Working...