/**
   * Chooses exactly one random element from successive blocks of <tt>weight</tt> input elements
   * each. For example, if weight==2, and the input is 5*2=10 elements long, then chooses 5 random
   * elements from the 10 elements such that one is chosen from the first block, one from the
   * second, ..., one from the last block.
   *
   * @param acceptList a bitvector which will be filled with <tt>true</tt> where sampling shall
   *     occur and <tt>false</tt> where it shall not occur.
   */
  private void xsampleNextElements(BooleanArrayList acceptList) {
    // manually inlined
    int length = acceptList.size();
    boolean[] accept = acceptList.elements();
    for (int i = 0; i < length; i++) {
      if (skip > 0) { // reject
        skip--;
        accept[i] = false;
        continue;
      }

      if (nextTriggerPos == UNDEFINED) {
        if (weight == 1) nextTriggerPos = 0; // tuned for speed
        else nextTriggerPos = generator.nextIntFromTo(0, weight - 1);

        nextSkip = weight - 1 - nextTriggerPos;
      }

      if (nextTriggerPos > 0) { // reject
        nextTriggerPos--;
        accept[i] = false;
        continue;
      }

      // accept
      nextTriggerPos = UNDEFINED;
      skip = nextSkip;
      accept[i] = true;
    }
  }
  /**
   * Randomly permutes the part of the receiver between <code>from</code> (inclusive) and <code>to
   * </code> (inclusive).
   *
   * @param from the index of the first element (inclusive) to be permuted.
   * @param to the index of the last element (inclusive) to be permuted.
   * @exception IndexOutOfBoundsException index is out of range (<tt>size()&gt;0 && (from&lt;0 ||
   *     from&gt;to || to&gt;=size())</tt>).
   */
  public void shuffleFromTo(int from, int to) {
    if (size == 0) return;
    checkRangeFromTo(from, to, size);

    org.ihtsdo.cern.jet.random.Uniform gen =
        new org.ihtsdo.cern.jet.random.Uniform(
            new org.ihtsdo.cern.jet.random.engine.DRand(new java.util.Date()));
    Object tmpElement;
    Object[] theElements = elements;
    int random;
    for (int i = from; i < to; i++) {
      random = gen.nextIntFromTo(i, to);

      // swap(i, random)
      tmpElement = theElements[random];
      theElements[random] = theElements[i];
      theElements[i] = tmpElement;
    }
  }
  /**
   * Chooses exactly one random element from successive blocks of <tt>weight</tt> input elements
   * each. For example, if weight==2, and the input is 5*2=10 elements long, then chooses 5 random
   * elements from the 10 elements such that one is chosen from the first block, one from the
   * second, ..., one from the last block.
   *
   * @return <tt>true</tt> if the next element shall be sampled (picked), <tt>false</tt> otherwise.
   */
  public boolean sampleNextElement() {
    if (skip > 0) { // reject
      skip--;
      return false;
    }

    if (nextTriggerPos == UNDEFINED) {
      if (weight == 1) nextTriggerPos = 0; // tuned for speed
      else nextTriggerPos = generator.nextIntFromTo(0, weight - 1);

      nextSkip = weight - 1 - nextTriggerPos;
    }

    if (nextTriggerPos > 0) { // reject
      nextTriggerPos--;
      return false;
    }

    // accept
    nextTriggerPos = UNDEFINED;
    skip = nextSkip;

    return true;
  }
Example #4
0
 /**
  * Sets the uniform random number generation engine shared by all <b>static</b> methods.
  *
  * @param randomGenerator the new uniform random number generation engine to be shared.
  */
 public static void staticSetRandomEngine(RandomEngine randomGenerator) {
   synchronized (shared) {
     shared.setRandomGenerator(randomGenerator);
   }
 }
Example #5
0
 /**
  * Returns a uniformly distributed random number in the closed interval <tt>[from,to]</tt>
  * (including <tt>from</tt> and <tt>to</tt>). Pre conditions: <tt>from &lt;= to</tt>.
  */
 public static long staticNextLongFromTo(long from, long to) {
   synchronized (shared) {
     return shared.nextLongFromTo(from, to);
   }
 }
Example #6
0
 /**
  * Returns a uniformly distributed random number in the closed interval <tt>[from,to]</tt>
  * (including <tt>from</tt> and <tt>to</tt>). Pre conditions: <tt>from &lt;= to</tt>.
  */
 public static int staticNextIntFromTo(int from, int to) {
   synchronized (shared) {
     return shared.nextIntFromTo(from, to);
   }
 }
Example #7
0
 /**
  * Returns a uniformly distributed random number in the open interval <tt>(from,to)</tt>
  * (excluding <tt>from</tt> and <tt>to</tt>). Pre conditions: <tt>from &lt;= to</tt>.
  */
 public static float staticNextFloatFromTo(float from, float to) {
   synchronized (shared) {
     return shared.nextFloatFromTo(from, to);
   }
 }
Example #8
0
 /**
  * Returns a uniformly distributed random number in the open interval <tt>(from,to)</tt>
  * (excluding <tt>from</tt> and <tt>to</tt>). Pre conditions: <tt>from &lt;= to</tt>.
  */
 public static double staticNextDoubleFromTo(double from, double to) {
   synchronized (shared) {
     return shared.nextDoubleFromTo(from, to);
   }
 }
Example #9
0
 /**
  * Returns a uniformly distributed random number in the open interval <tt>(0,1)</tt> (excluding
  * <tt>0</tt> and <tt>1</tt>).
  */
 public static double staticNextDouble() {
   synchronized (shared) {
     return shared.nextDouble();
   }
 }
Example #10
0
 /** Returns a uniformly distributed random <tt>boolean</tt>. */
 public static boolean staticNextBoolean() {
   synchronized (shared) {
     return shared.nextBoolean();
   }
 }