Quiz: Computing Pi with Darts
There are many ways of computing uniformly distributed floating-point numbers between -1 and 1, so if your answer is close to pi but not what the tester wants, feel free to peek at the answer video. Or try this hint first: What is 2 * generator.nextDouble()?
A few people wondered whether it is important to generate -1 or 1, or both. It is not. The probability for either to occur is zero! What you want for a uniform distribution is that P(a <= r < b) = P(a <= r <= b) = (b - a) / 2, where r is a random number and a <= b are bounds of an arbitrary interval.