Question:
Java question regarding creating a random length?
Alan
2009-12-17 19:40:30 UTC
Okay, so I'm supposed to create a random password with a random length using java and so far I've come up with the method:
public static char random63Characters()
{
String password = "";
for(int i = 0; i < DEFAULT_PWD_SIZE; i++)
{
double r = Math.random();
if(r < 26.0/63.0)
return randomUppercase();
else if (r < 52.0/63.0)
return randomLowercase();
else if (r < 62.0/63.0)
return randomDigit();
else
return '_';
}
return ;
}

The methods each return what they say(i.e randomDigit returns a number from 0 - 10 and randomUppercase returns A - Z) and the DEFAULT_PWD_SIZE is 6.
It works fine without the for statement, but then it only prints out one character. I'm kind of having trouble with this, so please help.
Three answers:
modulo_function
2009-12-17 22:05:49 UTC
If that method is supposed to return a String then this should work:



public static String random63Characters()

{

String password = "";

for(int i = 0; i < DEFAULT_PWD_SIZE; i++) {

...double r = Math.random();

...if(r < 26.0/63.0)

......password += randomUppercase();

...else if (r < 52.0/63.0)

......password += randomLowercase();

...else if (r < 62.0/63.0)

......password += randomDigit();

...else

String += '_';

...}

return password;

}



Is that what you wanted? It will return a password that's 6 chars long. But, where are you getting

the methods random UpperCase, LowerCase, and Digit?
SPB
2009-12-17 19:49:10 UTC
The for loop is irrelevant. No matter what "r" is you return after you get the first value for r. Instead of return statements, why don't you add the value from the randomXXXXX() methods to a StringBuffer. Then, aftre the for loop completes you return that StringBuffer as a String.



Oh, since your for loop has a constant as an ending value you will never generate a random length password. In order to do that you better end your for loop based on a value returned from Math.random().
gaffke
2016-10-31 11:28:40 UTC
The technique defined interior the above answer is technically superb, yet provides no assure on working time. In practice this is fairly quickly (a minimum of with a itemizing as short as length 10, it gets worse extremely with out postpone as you scale up), yet in theory it might desire to flow on working constantly, only happening to never discover a quantity it hadn't yet generated. right here's a fashion which will run slower in practice, yet does provide a assure on working time. save a itemizing of boolean flags with an identical length because of the fact the checklist you're determining on from, and start up with them waiting to faux. Then somewhat of producing a random quantity interior the kind, generate a random quantity Q from 0 to a million below the relax kind of things that have not been picked yet. Then iterate up in the process the checklist of flags with the intention to discover the Qth quantity between people who have not yet been picked (and once you discover it, set that flag to genuine so which you will no longer get it returned). For producing M products from a itemizing of length N, this methodology is absolute to run in O(MN) time (for simplicity we are going to anticipate you % to generate N products wherein case this is only quadratic time). right here's a pair of approaches demonstrating the theory: public static int gen_index(boolean[] flags,int ultimate) { int x=(int)(Math.random() * (double)ultimate); int y=0; collectively as(y0) { n--; chanced on[n] = checklist[gen_item(flags, ultimate)]; ultimate--; } return chanced on; } i've got not somewhat examined this code, yet a minimum of you will see what's happening right here and why the theory works. i'm able to envision a fashion that could artwork even swifter on rather long lists whilst N is on the edge of M, besides the shown fact that it would not look which you somewhat % any such component superb now.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...