how to generate number and characters together randomly. I know how to generate numbers randomly, but not getting anywhere with random character generation.

Please help..

Thanks

how to generate number and characters together randomly. I know how to generate numbers randomly, but not getting anywhere with random character generation.

Please help..

Thanks

If you can pick a random letter and you pick a random integer, you can pick a random letter or integer, depending on the probability distribution you want. An example:

int stringLength;
String randomString;

for (int i = 0; i < stringLength; i++)
{
    char c;
    int randomNumber = /* number from 0 to 35 */
    if (randomNumber < 10)
        c = /* pick random digit */
   else
        c = /* pick random letter */

   // add c to end of randomString
}

If you are trying to make some kind of encryption or possibly a random password maker u can use random ascii decimal values between 32-126. (look at http://www.asciitable.com/)

all you would have to do is generate random integers between that range and type cast them into chars, saving them into a string.

If you are trying to make some kind of encryption or possibly a random password maker u can use random ascii decimal values between 32-126. (look at http://www.asciitable.com/)

Hey Java uses UNICODEs and not ASCII values

I really don't like all these hacks based on the internal representation of chars, and what about the non-alphanumeric chars that sit in spaces between the alphabetics in ASCII/Unicode? Here's a good Java way to do it:
Create a char array containing all the valid symbols you want to use (digits, upper/lower case letters, selected punctuation etc). Use your random integers as indexes into the array to select random chars.

... or (for a more compact source listing), put all the valid chars in a single String, and use random charAt(...) calls to get random valid chars.

String validChars = "123abcABC&!?";
Random r = new Random();
for (as many chars as you want) {
    char randomChar = validChars.charAt(r.nextInt(validChars.length()));
}
This article has been dead for over six months. Start a new discussion instead.