StdRandom { public static int discrete(double[] a) { double r = uniform(); double sum = 0.0; for (int i = 0; i < a.length; i++) { sum = sum + a[i]; if (sum >= r) return i; } assert false; return -1; } private  StdRandom(); static void setSeed(long seed); static double uniform(); static double random(); static int uniform(int N); static int uniform(int a, int b); static double uniform(double a, double b); static boolean bernoulli(double p); static boolean bernoulli(); static double gaussian(); static double gaussian(double mean, double stddev); static int geometric(double p); static int poisson(double lambda); static double pareto(double alpha); static double cauchy(); static int discrete(double[] a); static double exp(double lambda); static void shuffle(Object[] a); static void shuffle(double[] a); static void shuffle(int[] a); static void shuffle(Object[] a, int lo, int hi); static void shuffle(double[] a, int lo, int hi); static void shuffle(int[] a, int lo, int hi);  }
@Test public void testDiscrete_1() throws Exception { double[] a = new double[] { 1.0 }; int result = StdRandom.discrete(a); }