2

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.

Community
  • 1
  • 1
EGHDK
  • 17,818
  • 45
  • 129
  • 204

3 Answers3

5

Well it doesn't look too bad to me. Let's create a more statistically significant test:

import java.util.Random;

public class Test {
    public static void main(String[] args) throws Exception {

        Random rng = new Random();
        int total = 0;
        for (int i = 0; i < 1000000; i++) {
            total += rng.nextInt(2);
        }
        System.out.println("Total: " + total);
    }
}

Sample output from 5 runs:

Total: 501184
Total: 499740
Total: 500116
Total: 500374
Total: 500413

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • What's the significance of `total += rng.nextInt(2);` I'm really asking about the "2" at the end there. Still consider myself fairly new to java. Thanks. – EGHDK Nov 11 '12 at 18:05
  • @EGHDK: Well it matches your original code - it's an exclusive upper bound. Read the documentation for `Random.nextInt`. Basically that call will either return 0 or 1. I'm adding up the results, so you'd expect it to be about half of the number of iterations. – Jon Skeet Nov 11 '12 at 18:09
  • Interesting. Alright, thanks! – EGHDK Nov 11 '12 at 18:10
1

Do that 10000 times and it will approach 50:50.

Then toss a coin 9 times - the result will probably be similar to what random.nextInt() gives you.

The key here is to statistically significant amount of data.

And btw, use random.nextBoolean()

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
-1

You need to add a seed to your random class. Typically it's a timestamp. So try something like

Random generator = new Random(System.currentTimeMillis);
donebizkit
  • 458
  • 3
  • 9
  • You have to be careful with that, as you might be seeding all your "random" generators with the same seed. @EGHDK's best bet would actually to be to create as few Random instances as possible. – Neil Nov 11 '12 at 18:39
  • This will not help - it is actually worse than the default constructor (which uses a timestamp **and** a static counter.) – finnw Nov 12 '12 at 19:24