Random Strings help!
So I have this problem and it is completely stumping me..
Here is what I have to do so you can get an idea : I need to make a program that will read a string and long from the keyboard. Then I have to use the long as a seed for creating a Random object.
Now, using the Random object, I need to generate two random numbers. The first random number will be the starting position for creating a substring from the original string, and, the second random number will be the end point of the string. Next, the two numbers will be used to extract a substring from the original string. Finally, the random numbers should be generated so the substring extracted contains at least two characters.
My teacher gave us this problem and Im not skilled enough to figure it completely out..
That is what I have so far... butcher it please.. any help is awesome! Thanks!
static Random seed = new Random();
public static void main (String  args )
Scanner scan = new Scanner( System.in);
System.out.print(" Enter a string: ");
String first = scan.nextLine();
System.out.print(" Enter a seed: ");
long alpha = scan.nextLong();
System.out.println( "\n The original string: " + first );
Random generator1 = new Random();
int r = generator1.nextInt();
Random generator2 = new Random();
int s = generator2.nextInt();
System.out.println( "\n Creating a substring from " + r + " to " + s);
Random has another nextInt method, namely one where you provide the "maximum" value, so provide that the length of the String (-1 so the substring will be at least 1 character long) the first time, and the length of the String - the first number the second time (then the end index will be the first number plus the second number and the second index has no chance of being either beyond the end of the String, nor smaller than the first index).
And, why are you not using the Random constructor that takes a long as the seed, since the instructions plainly state that you should be doing this.
Ok so Im going back over my notes/book for info on the Random class, I probably missed something..
I'm taking this one step at a time.. but for the output, I can get it to repeat the original string.. but I also have to state a random substring, the random substring in all CAPS, the random substring will all letters lower-case and the random substring with the first and last letter capitalized...
I feel like I'm missing a good bit of info on the output of the program.. I have most of the input correct up till the Random class. Any pointers?
Appreciate the help
I think I am in your class... I am having the same issue getting the random # to be 2 characters in length.
Okay? So try to implement those suggestions, and post your modified code here.
Originally Posted by hhale
Random (Java Platform SE 6)
Edit: As far as upper casing there is a method in String.
String (Java Platform SE 6)
Edit: And, if it is to be at least 2 characters (rather than at least one which I pointed out how to do), that should be a rather simple adjustment to my previous suggestions.
In addition to the above, use only one Random() object. If you have two (as you have), each seeded with the same long, then nextInt() will return the same value on the first as the second.
You have a perfectly good one in the static "seed" at the top.