473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 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 8339
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 source below. ...
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 built right into the .NET Framework. I was...
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 the current time as the random seed, but it turns...
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 want to print unique random number how to print by...
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 pointers on the same? Thanks, Avanti
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 http://en.wikipedia.org/wiki/Random_number_generator. ...
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 random numbers. I know if we want to generate a...
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 password is generated without having to fill in the...
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 relevant files from the firefox .xpi file and the .jar...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.