11-14-2009, 11:02 AM
(11-07-2009, 12:26 PM)Project Evolution Wrote: Done version 2 I guess, trying to figure out character randomization, and for now im using the Random API for randomizing the numbers.
Think about it... what's a simple way of having numbers correspond to any element? If you guessed array, you're right.
You could set up an array of 26 characters, and then initialize them with a for loop. shouldn't take more than three lines of code.
Code:
char letters[26];
for(int i = 0; i < 26; i++) {
letter[i] =(char) i + 97; //ascii key codes, 'a' is 97.
}
the iffy part here is the type-casting, I think java allows casting from an int to a char (I mean, why wouldn't they), but if not, I'm sure there's a method for turning an int into a char.
from there, you can get random characters by using pseudorandom elements in the array.
i.e...
Code:
char nextChar = letters[Random_generator.nextInt(25)]; //probably wrong syntax, but you know what I mean...
password.append(letter);