来自于Effective Java 2nd Item47 Know and use the libraries
import java.util.Random;
public class Test {
private static final Random rnd = new Random();
public static void main(String[] args) {
int n = 2 * (Integer.MAX_VALUE / 3);
int low = 0;
for (int i = 0; i < 1000000; i++)
if (random(n) < n / 2)
low++;
System.out.println(low);
}
// Common but deeply flawed!
static int random(int n) {
return Math.abs(rnd.nextInt()) % n;
}
}
结果是什么,猜一下,然后再运行一下,为什么?