/**
   * 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;
  }
示例#4
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);
   }
 }