rand() - rand() Likely to be Zero?

This is a weird bit of programming logic that I randomly found in my Artificial Intelligence for Games book:

Apparently, the following function:

1
2
3
float randomBinomial() {
    return random() - random();
}


... where random() returns a value between 0 and 1, is more likely to return values near 0 than values near -1 or 1. (That is, randomBinomial returns closer to 0.) There are no comments made about the implementation of random()'s generator, it's implied that it holds true given any random generator. Why would it return values closer to 0?

* Edit for correctness
Last edited on
Lets say that random can be either 0, 1 or 2 and list all possible outcomes:

0 - 0 = 0
1 - 0 = 1
2 - 0 = 2
0 - 1 = -1
1 - 1 = 0
2 - 1 = 1
0 - 2 = -2
1 - 2 = -1
2 - 2 = 0

P(0) = 3/9
P(1) = 2/9
P(-1)= 2/9
P(2 )= 1/9
P(-2)= 1/9
I was literally just doing this separately.
Ninja
In a perfect world, this forum would have a Like button.
Topic archived. No new replies allowed.