Random Numbers (Argh!)
Hi everybody!
I haven't used random numbers that often in Java and today I was trying to use them. I'm making a program generates many random numbers and holds on to them for later reference. My strategy was to create an array of the Random class by using a for loop, then store those random numbers to double variables like such.
Random[] rand = new Random[str.length()];
double[] val = new double[str.length()];
for (int x = 1; x < str.length(); x++) {
rand[x] = new Random();
val[x] = rand[x].nextDouble();
}
Now I did this, and to my surprise <insert haunting music here> all the numbers popped up the same! I'm learning / have learned Java from Sam's Teach Yourself Java 2. That book kind of touches on random numbers and how they are really "pseudo-random", and I understand that and all, but it never tells how to get around the "pseudo-randomness". I'm guessing that my problem is that Java works it's magic so fast that all the numbers are generated at virtually the same instant.
I have tried to make the numbers different by inserting a pointless time wasting loop inside the for loop but I don't want it to take forever. I tried inserting the variable x as a seed in the 5th line. Nothing worked that well. Any ideas? HAve I overlooked the simple solution?
Thans a million!
~Matt

