I don't really understand your question but if you want to automate the generation of the password you can use the userid as password the first time the user id is created and next time the same user log on to your application he/she will be prompted to change the password according to the rules you set up.
Klint
Here's the code:
public class RandomPassword
{
int l = 0;
int h = 5;
String charr[] = {"A", "B", "C", "1", "2", "3"};
private String[] rarr = new String[6];
RandomPassword()
{
buildPassword();
printPassword();
}
private void buildPassword()
{
int n;
for (int i = 0; i <= 5; i++)
{
n = getRandom(l, h);
rarr = charr[n];
}
}
private void printPassword()
{
for (int i = 0; i <= 5; i++)
System.out.print(rarr);
System.out.println();
}
private int getRandom( int lower, int upper )
{
double nNum = Math.random();
int nDif,nResult;
while ( true )
{
nDif = (upper + 1) - lower;
nResult = lower + (int)( (double)nDif * nNum );
if ( ( nResult >= lower ) && ( nResult <= upper ) )
{
return nResult;
}
}
}
public static void main(String args[])
{
try
{
new RandomPassword ();
System.exit(0);
}
catch ( Exception e )
{
System.out.println ( "Can not start program");
}
}
}
Klint
Tsang,
This one is even better.
public class RandomPassword2
{
private String base;
private int len;
private String pwd;
private char [] charr;
private char [] rarr;
public RandomPassword2(String b, int l)
{
base = b;
len = l;
}
public RandomPassword2()
{}
public String getRandomPassword()
{
buildPassword();
return pwd;
}
private void buildPassword()
{
charr = new char[base.length()];
charr = base.toCharArray();
rarr = new char[len];
int n;
for (int i = 0; i <= len - 1; i++)
{
n = getRandom(0, base.length() - 1);
rarr = charr[n];
}
pwd = new String(rarr);
}
private int getRandom( int lower, int upper )
{
double nNum = Math.random();
int nDif,nResult;
while ( true )
{
nDif = (upper + 1) - lower;
nResult = lower + (int)( (double)nDif * nNum );
if ( ( nResult >= lower ) && ( nResult <= upper ) )
{
return nResult;
}
}
}
}
Klint
Michael,
I do agree but in some instances it might be required to generate a random password. In the environment I am woring in, AS400, you get by default the user id as password when you create the user. When the user log on the AS400 he/she is prompted to change the password according to the rules you set.
Klint