/** * 2 methods below, 1st one entire, 2nd one skeleton * yes, for generate, could/might set the x/y variables outside of method * * My goal, which I can't figure out how to do is to PASS the value of * variable "rangeX" created in generate() to be the value of "length" * for method 2 * have messed around for quite awhile, not getting anywhere, still newbie * understand scope of variables is somehow hindering this process but not * sure what to do. * * Thank you! * */ /////////////////////////// //////////////////////////////// private static int generate(int x, int y) { x = 5; y = 6; int range = y - x + 1; int rangeX; //return (Math.random() * range) + x; rangeX = (int)(Math.random() * range) + x; return rangeX; } /////////////////////////// //////////////////////////////// /////////////////////////// //////////////////////////////// private static String randomString (int length) { } /////////////////////////// //////////////////////////////// /////////////////////////////////////////////////////////// import java.util.*; public class Test { private final static char[] chars; private final static Random random; private static int length; // private static int rangeX; static { chars = new char[52]; for (int i = 0; i < 26; i ++) { chars[i] = (char) (97 + i); chars[i + 26] = (char) (65 + i); } random = new Random (); //length = random.nextInt(1)*5 + 5; //length = (int)(4 + Math.random() * 3); //length = (5 || 6); } public static void main (String[] args) { generate(5,6); System.out.println (randomString (length)); } // int random = (int)(Math.random()*40); // int random = new java.util.Random().nextInt(40); private static int generate(int x, int y) { x = 5; y = 6; int range = y - x + 1; int rangeX; //return (Math.random() * range) + x; rangeX = (int)(Math.random() * range) + x; return length = rangeX; } private static String randomString (int length) { //tried to convert into using Math.random instead of having to import // Class RANDOM but MATH uses DOUBLES, and since with // Arrays when setting the # of elements, you must use INT, seems like it'd be //best/only possible?/efficient to use class Random instead of Math.random in //this case? char[] array = new char[length]; for (int i = 0; i < length; i ++) { array[i] = chars[random.nextInt (chars.length)]; } return new String (array); } }