Click to See Complete Forum and Search --> : random phrase


jrthor2
03-29-2007, 07:21 AM
How can I generate a random phrase that is 5 characters long, and contains only letters a-z and numbers 0-9?

Thanks

Khalid Ali
03-30-2007, 01:36 AM
its fairly simple,
There are several approaches you can take.
1. Create some phrases and put them in a list a string array.
Then generate a random number using Random class. This random number will
will always be less then the length of the array and will give u a phrase
2. You can actually create the whole phrase randomly by using the same logic
only this time u pick a random number and pick a character or a number
between ur limits

jrthor2
03-30-2007, 06:53 AM
Any chance of getting a code example?

Thanks!

Cheater
04-12-2007, 09:56 PM
This isn't a code example, but it should make it quite simple to do.
Generate a random number from 1-36. Then, do a switch/if else to check the value. If it is 1, the character is a 2=b, 3=c, ..., 27=0, 28=1, ..., 36=9. I don't know what you intend to do with the password. If you just want to print it to the terminal/command prompt, you could use a for loop to generate 5 characters using the method above, and print each one. Otherwise, you should be able to figure out what to do.

agent_x91
04-15-2007, 03:18 PM
Woah, cheater that's extremely long, unnecessary, and a very poor way of doing it.

Best way to do it is decide a minimum and maximum ASCII value for the range of characters used, for example:


int lower_bound=(int)'a';
int upper_bound=(int)'9';

String s="";

for(int i=0;i<10;i++)
s+=(char)Math.floor(Math.random()*(upper_bound-lower_bound))+lower_bound;


The random string will be 10 characters long, you can change this simply by changing the maximum value for i.