Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Random Characters??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Random Characters???" Watch "Random Characters???" New topic
Author

Random Characters???

Sam Bluesman
Ranch Hand

Joined: Nov 21, 2004
Posts: 191
Is it possible to generate a random character in a similar way it is to generate a random number using, say, the system clock, or would it be better to set up an array that holds all the characters in and work through that? I need to use single characters as a unique identifier you see.

Cheers


Moosey knows best
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You could generate random integers that are between say 65 and 90 and then convert them to a character.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Sam Bluesman:
Is it possible to generate a random character in a similar way it is to generate a random number using, say, the system clock, or would it be better to set up an array that holds all the characters in and work through that? I need to use single characters as a unique identifier you see.

Cheers


Either way sounds feasible. The algorithm you choose will depend on the requirements and what kind of "randomness" you desire. Do you need to avoid duplicates? If so, then you probably want to use a List of available chars and remove the ones that have been used. This is a fairly common way to pick anything at random without duplication.

On the other hand, if you don't care about duplicates, generating a random number between 'a' and 'z'. Since characters are really numerical values that use the Unicode character set to translate into a char, you can generate numbers between the Unicode values of 'a' and 'z'. So you might want to start this approach by writing a small methd that returns a random number between some min and max values that are passed in as parameters.

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Random Characters???
 
Similar Threads
Random Password Generation Tool?
generate a text file with random no of characters
random password generation
Generate next hexadecimal?
Random Numbers