468,761 Members | 1,806 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,761 developers. It's quick & easy.

Generating a random alphanumeric sequence

94
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.

Expand|Select|Wrap|Line Numbers
  1. public static String plate()
  2. {
  3.     int x = 6;
  4.     char[] plate = new char[x];
  5.     int c  = 'A';            
  6.     for(int p = 0; p < 6; p++)
  7.     {
  8.         int vehiclePlate = 0 + (int) (Math.random()* 6);
  9.         switch(vehiclePlate)
  10.         {
  11.             case 0: c = '0' +  (int)(Math.random() * 10); break;
  12.             case 1: c = 'A' +  (int)(Math.random() * 26); break;
  13.         }
  14.         plate[p] = (char)c;
  15.     }
  16.     return new String(plate);
  17. }
  18.  
Any help, suggestions or links to any relevant info is appreciated. Thanks.
Mar 8 '07 #1
5 7874
Ganon11
3,652 Expert 2GB
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.
Mar 8 '07 #2
dav3
94
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.
Mar 8 '07 #3
Ganon11
3,652 Expert 2GB
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.
Mar 8 '07 #4
dav3
94
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.
Mar 11 '07 #5
Ganon11
3,652 Expert 2GB
No problems, glad to help!
Mar 12 '07 #6

Post your reply

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

Similar topics

12 posts views Thread by 9966 | last post: by
1 post views Thread by G Love | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.