472,328 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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 8168
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

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

Similar topics

1
by: Intaek LIM | last post by:
generally, we use srand(time(0)) to generate random numbers. i know why we use time(0), but i can not explain how it operates. first, see example...
2
by: Joe | last post by:
Hi, I am building web in ASP.NET using VB.NET to code the pages. I want to generate random passwords for users. I know that password hashing is...
2
by: randomcz | last post by:
hi, i need help with generating random numbers; The task is to generate hundreds of random vectors, like 1,3,5,6,7 2,4,5,4,8 ... I used...
1
by: Velhari | last post by:
Hi, I am a beginner. Please tell me, For generating Random Numbers, Initially why we are going for seed method. And another question is that, I...
14
by: avanti | last post by:
Hi, I need to generate random alphanumeric password strings for the users in my application using Javascript. Are there any links that will have...
0
SammyB
by: SammyB | last post by:
These are some "random" thoughts about generating random numbers in Visual Basic. Wikipedia will give a better introduction than I, see...
12
by: 9966 | last post by:
Greetings, This is my second post till now. Thanks for all the advice given to me for the first post. Now I'm having problem with generating...
1
by: Krimp | last post by:
I pulled this code from Vbasic.net or generating random passwords. I want to know how I can set up so that each time the page is refreshed a new...
1
by: G Love | last post by:
Hi, I am trying to modify some code in a toolbar I have for generating random 'spam' entries on my forum. I have managed to extract all the...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.