产生两个随机整数,比较是否同为奇数或同为偶数,最后计算相同的概率,代码如下:import java.util.*;public class TestRandom {
    private static final int COUNT = 50000;
    TestRandom(){
        Random r = new Random();
        int cnt = 0;
        for(int i=0;i<COUNT;i++){
            int m = r.nextInt() % 2;
            int n = r.nextInt() % 2;
            if(m == n){
                cnt++;
            }
        }
        System.out.println("rate:" + ((double)cnt/ COUNT));//预期的结果应该是0.5左右,但打印出来的却是0.375左右
    }    public static void main(String[] args) {
        TestRandom r = new TestRandom();
    }
}
随机产生的两个整数,无非4种情况:
奇、偶
偶、奇
奇、奇
偶、偶
他们同为奇数或者偶数的概率是0.5,但是结果却是0.375左右,不清楚什么原因。
Java产生的随机数是伪随机的,但是应该也是符合正态分布的,或者还有其他分布规律的,但是这个问题的结果不是0.5,所以很疑惑,期待有人能解释,谢谢!

解决方案 »

  1.   

    使用两个 Random 
    next 是使用上一次数做种子算法相关,不可能保证是 50%
      

  2.   

    private static final int COUNT = 50000; public static void main(String[] args) {
    Random r = new Random();
    int cnt = 0;
    for (int i = 0; i < COUNT; i++) {
    int m = r.nextInt(2);
    int n = r.nextInt(2);
    if (m == n) {
    cnt++;
    }
    }
    System.out.println("rate:" + ((double) cnt / COUNT));
    }这样可以
    不过我也不清楚为什么
      

  3.   


    public class TestRandom {
        private static final int COUNT = 50000;
        TestRandom(){
            Random r = new Random();
            int cnt = 0;
            for(int i=0;i<COUNT;i++){
                int m = r.nextInt() % 2;// 这里的m和n有可能是-1,如果用楼上的nextInt(2),表示产生0-2之间的随机数,所以结果接近0.5
                int n = r.nextInt() % 2;
                if(Math.abs(m) == Math.abs(n)){
                    cnt++;
                }
            }
            System.out.println("rate:" + ((double)cnt/ COUNT));
        }    public static void main(String[] args) {
            TestRandom r = new TestRandom();
        }
    }nextInt
    public int nextInt(int n)
    Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. The general contract of nextInt is that one int value in the specified range is pseudorandomly generated and returned. All n possible int values are produced with (approximately) equal probability. The method nextInt(int n) is implemented by class Random as if by: 
     public int nextInt(int n) {
       if (n <= 0)
         throw new IllegalArgumentException("n must be positive");   if ((n & -n) == n)  // i.e., n is a power of 2
         return (int)((n * (long)next(31)) >> 31);   int bits, val;
       do {
           bits = next(31);
           val = bits % n;
       } while (bits - val + (n-1) < 0);
       return val;
     }
    The hedge "approximately" is used in the foregoing description only because the next method is only approximately an unbiased source of independently chosen bits. If it were a perfect source of randomly chosen bits, then the algorithm shown would choose int values from the stated range with perfect uniformity. The algorithm is slightly tricky. It rejects values that would result in an uneven distribution (due to the fact that 2^31 is not divisible by n). The probability of a value being rejected depends on n. The worst case is n=2^30+1, for which the probability of a reject is 1/2, and the expected number of iterations before the loop terminates is 2. The algorithm treats the case where n is a power of two specially: it returns the correct number of high-order bits from the underlying pseudo-random number generator. In the absence of special treatment, the correct number of low-order bits would be returned. Linear congruential pseudo-random number generators such as the one implemented by this class are known to have short periods in the sequence of values of their low-order bits. Thus, this special case greatly increases the length of the sequence of values returned by successive calls to this method if n is a small power of two. 
    Parameters:
    n - the bound on the random number to be returned. Must be positive. 
    Returns:
    the next pseudorandom, uniformly distributed int value between 0 (inclusive) and n (exclusive) from this random number generator's sequence 
    Throws: 
    IllegalArgumentException - if n is not positive
    Since: 
    1.2 
      

  4.   

    nextint有负有正
    模2出来有三种可能:
    -1 0 1
    改成
    int m = Math.abs(r.nextInt()) % 2;
    int n = Math.abs(r.nextInt()) % 2;
    才行