第二天打卡。

random int value drawn from discrete distribution (i with probability a[i])

public static int double(double[] a) {
    double r = StdRandom.random();
    double sum = 0.0;
    for(int i = 0; i < a.length; i ++) {
        sum += a[i];
        if(sum >= r) return i
    }
    return -1;
}
View Code

randomly shuffle the elements in an array of double values

public static void shuffle(double[] a) {
    int N = a.length;
    for (int i = 0; i < N; i ++) {
        int r = i + StdRandom.uniform(N-i);
        double temp = a[r];
        a[r] = a[i];
        a[i] = temp;
    }
}
View Code
原文地址:https://www.cnblogs.com/moonlightshadow/p/7283851.html