Click to See Complete Forum and Search --> : Random Password Generator


WillemM
January 13th, 2003, 03:46 AM
I have the following code for generating random passwords:



int RandomNumber(int Start, int End)
{
return (int)(Start + Math.random() * (End - Start));
}

String RandomPassword(String passwordSeed, int Length)
{
int i;
String s;
int x;
String temp = "";

for(i = 0; i < Length; i++)
{
x = RandomNumber(0,passwordSeed.length());
s = passwordSeed.substring(x,1);
temp += s;
}

return temp;
}



When I call the function RandomPassword it gives me StringIndexOutOfBoundsException :(
What is the solution ? The index is generated correct (as far as I know). So there must be something else going wrong.

dlorde
January 13th, 2003, 04:09 AM
The second argument to String.substring(int beginIndex, int endIndex) is the end index of the substring - the substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Passing 1 for the end index means you're mostly asking for a substring of negative length...


I'm not afraid of dying - I just don't want to be there when it happens...

swaroopkg
January 13th, 2003, 10:54 PM
The best way to give would be
s=passwordseed.substring(x, x+1)

WillemM
January 14th, 2003, 01:13 AM
LOL, Yeah that did the trick :) Thanks for all the help.

P.S. Java isn't soo hard to learn :D