Ejemplo n.º 1
0
  public static Rational[] probVector(int length, Random prng) {
    if (length == 0) return new Rational[] {};
    else if (length == 1) return new Rational[] {Rational.ONE};

    double dProb = prng.nextDouble();
    Rational probA = Rational.valueOf(dProb);
    Rational probB = Rational.valueOf(1 - dProb);
    if (length == 2) {
      return new Rational[] {probA, probB};
    } else {
      Rational[] a = probVector(length / 2, prng);
      Rational[] b = probVector((length + 1) / 2, prng);
      Rational[] c = new Rational[a.length + b.length];
      for (int i = 0; i < a.length; ++i) {
        c[i] = a[i].multiply(probA);
      }
      for (int i = 0; i < b.length; ++i) {
        c[a.length + i] = b[i].multiply(probB);
      }
      return c;
    }
  }