Possible Duplicate:
True random generation in Java
Java random always returns the same number when I set the seed?
I run this piece of code in one of my programs.
public String[] gen_phase_zero() {
Random generator = new Random();
int r = generator.nextInt(2);
if (r == 1) {
String big = "A";
String small = "a";
return new String[] { big, small };
} else {
String big = "B";
String small = "b";
return new String[] { big, small };
}
}
If I run this a few times my output is like this.
Aa
Aa
Aa
Aa
Bb
Aa
Aa
Aa
Bb
It’s not alwasy in that order. But it’s almost never anything close to 50/50
Update:
I’m not expeccting fifty fifty, but it seems that if “Aa” is selected first, then it’ll be next around 3 more times, but if Bb is selected first, it’ll be selected the next three times as well.
Well it doesn’t look too bad to me. Let’s create a more statistically significant test:
Sample output from 5 runs:
Doesn’t look hugely biased to me…
I got the same results when calling
new Random()inside the loop instead of just once, too – although it’s not a good idea to do that.