random int value drawn from discrete distribution (i with probability a[i])
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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; }
randomly shuffle the elements in an array of double values
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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; } }