sorry, a minor change... if u want numbers between 1-9 only,
change
s += (int)(Math.random()*10);
to
s += (int)(Math.random()*9 + 1);
vemul
Main Topics
Browse All TopicsHi, all
I am creating a web site with JSP. Could you please tell me how to generate a random string which consists of any number or any characters (1-9 or a-z or A-Z). Thanks.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
How long do you want the string to be? How about 40? Try this
import java.util.*;
class RandString{
public static void main(String[]args){
Random rand = new Random();
String [] charset = {"0","1","2","3","4","5","
"a","b","c","d","e","f","g
"A","B","C","D","E","F","G
StringBuffer sb = new StringBuffer();
for(int n=0;n<40;n++){
sb = sb.append(charset[rand.nex
}
System.out.println(sb.toSt
}
}
Heres a class I once wrote for just that purpose:
--------------------------
import java.util.Random;
/**
* This class can be used to generate strings of random characters.
* The length of the generated strings, as well as the charactes the strings
* are generated with can be specified
*
* @see java.util.Random
* @author Jan Louwerens
*/
public class RandomString
{
private static final String DEFAULT_SOURCE_CHARS = "abcdefghijklmnopqrstuvwxy
private Random random = null;
private String m_SourceChars = null;
/**
* Creates a RandomString instance with the source characters including
* lowecase letters a-z, uppercase letters A-Z, and numbers 0-9
*/
public RandomString()
{
this(DEFAULT_SOURCE_CHARS)
}
/**
* Creates a RandomString instance with the specified source characters.
* The sourceChars string must not be null and must contain at least one
* character
*
* @param sourceChars a string of characters from which a random string
* can be derived
* @throws IllegalArgumentException if sourceChars is null or does not
* contain at least one character
*/
public RandomString(String sourceChars)
throws IllegalArgumentException
{
if (sourceChars == null)
throw new IllegalArgumentException("
if (sourceChars.length() <= 0)
throw new IllegalArgumentException("
random = new Random();
m_SourceChars = sourceChars;
}
/**
* Returns a character chosen at random from the pool of sourceChars
*
* @return a character chosen at random from the pool of sourceChars
*/
public char nextChar()
{
int index = random.nextInt(m_SourceCha
return m_SourceChars.charAt(index
}
/**
* Returns a string of the specified length whose characters are chosen
* at random from the pool of sourceChars
*
* @param length the length of the generated string. must be >= 0
* @return a string of the specified length whose characters are chosen
* at random from the pool of sourceChars
* @throws IllegalArgumentException if length < 0
*/
public String nextString(int length)
throws IllegalArgumentException
{
if (length < 0)
throw new IllegalArgumentException("
StringBuffer results = new StringBuffer(length);
for (int index = 0; index < length; index++)
results.append(nextChar())
return results.toString();
}
/**
* Returns a character chosen at random from the pool of sourceChars<br>
* NOTE: If you wish to generate multiple characters, it is suggested
* that you create a RandomString instance and use the nextChar()
* method
*
* @return a character chosen at random from the pool of sourceChars
* @see RandomString()
* @see nextChar()
*/
public static char getRandomChar()
{
return getRandomChar(DEFAULT_SOUR
}
/**
* Returns a character chosen at random from the pool of sourceChars<br>
* NOTE: If you wish to generate multiple characters, it is suggested
* that you create a RandomString instance and use the nextChar(String)
* method
*
* @param sourceChars a string of characters from which a random character
* can be chosen
* @return a character chosen at random from the pool of sourceChars
* @see RandomString(String)
* @see nextChar(String)
*/
public static char getRandomChar(String sourceChars)
{
RandomString randomString = new RandomString(sourceChars);
return randomString.nextChar();
}
/**
* Returns a string of the specified length whose characters are chosen
* at random from the pool of sourceChars<br>
* NOTE: If you wish to generate multiple strings, it is suggested
* that you create a RandomString instance and use the nextString(int)
* method
*
* @param length the length of the generated string. must be >= 0
* @return a string of the specified length whose characters are chosen
* at random from the pool of sourceChars
* @throws IllegalArgumentException if length < 0
* @see RandomString()
* @see nextString(int)
*/
public static String getRandomString(int length)
throws IllegalArgumentException
{
return getRandomString(DEFAULT_SO
}
/**
* Returns a string of the specified length whose characters are chosen
* at random from the pool of sourceChars<br>
* NOTE: If you wish to generate multiple strings, it is suggested
* that you create a RandomString instance and use the
* nextString(String, int) method
*
* @param sourceChars a string of characters from which a random string
* can be derived
* @param length the length of the generated string. must be >= 0
* @return a string of the specified length whose characters are chosen
* at random from the pool of sourceChars
* @throws IllegalArgumentException if sourceChars is null or does not
* contain at least one character, or
* if length < 0
* @see RandomString(String)
* @see nextString(int)
*/
public static String getRandomString(String sourceChars, int length)
throws IllegalArgumentException
{
RandomString randomString = new RandomString(sourceChars);
return randomString.nextString(le
}
}
try this,
public class Random
{
public static String nextString()
{
StringBuffer message = new StringBuffer();
message.append( random.nextInt(1024) );
message.append( (char) ( random.nextInt( 26 ) + 65 ) );
message.append( (char) ( random.nextInt( 26 ) + 65 ) );
message.append( random.nextInt(1024) );
message.append( (char) ( random.nextInt( 26 ) + 65 ) );
message.append( (char) ( random.nextInt( 26 ) + 65 ) );
message.append( random.nextInt(1024) );
message.append( (char) ( random.nextInt( 26 ) + 65 ) );
message.append( (char) ( random.nextInt( 26 ) + 65 ) );
message.append( random.nextInt(1024) );
message.append( (char) ( random.nextInt( 26 ) + 65 ) );
message.append( (char) ( random.nextInt( 26 ) + 65 ) );
return message.toString();
}
}
Business Accounts
Answer for Membership
by: vemulPosted on 2002-10-21 at 13:50:09ID: 7353768
try this..
3;
class Test6
{
public static void main(String[] args)
{
String s = new String();
int i = (int)(Math.random()*10) + 5;
int rand = 0;
for(int x = 0; x < i; x++)
{
rand = ((int)(Math.random()*10))%
if( ( rand == 0))
s += (int)(Math.random()*10);
else if(( rand == 1))
s += (char)(Math.random()*26 +65);
else
s += (char)(Math.random()*26 +97);
}
System.out.println(s);
}
}
This code guarantees that the length of the string is atleast 5; could contain 1-9, a-z, A-Z. It does not guarantee that every string that is generated will be unique though
HTH
vemul