Esempio n. 1
0
  /**
   * 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) {
    // overridden for performance only.
    if (size == 0) {
      return;
    }
    checkRangeFromTo(from, to, size);

    cern.jet.random.Uniform gen =
        new cern.jet.random.Uniform(new cern.jet.random.engine.DRand(new java.util.Date()));
    short tmpElement;
    short[] 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;
    }
  }
Esempio n. 2
0
  /**
   * Uniformly samples (chooses) <tt>n</tt> random elements <i>with or without replacement</i> from
   * the contained elements and adds them to the given buffer. If the buffer is connected to a bin,
   * the effect is that the chosen elements are added to the bin connected to the buffer. Also see
   * {@link #buffered(int) buffered}.
   *
   * @param n the number of elements to choose.
   * @param withReplacement <tt>true</tt> samples with replacement, otherwise samples without
   *     replacement.
   * @param randomGenerator a random number generator. Set this parameter to <tt>null</tt> to use a
   *     default random number generator seeded with the current time.
   * @param buffer the buffer to which chosen elements will be added.
   * @throws IllegalArgumentException if <tt>!withReplacement && n > size()</tt>.
   * @see cern.jet.random.sampling
   */
  public synchronized void sample(
      int n,
      boolean withReplacement,
      RandomEngine randomGenerator,
      cern.colt.buffer.DoubleBuffer buffer) {
    if (randomGenerator == null) randomGenerator = cern.jet.random.Uniform.makeDefaultGenerator();
    buffer.clear();

    if (!withReplacement) { // without
      if (n > size()) throw new IllegalArgumentException("n must be less than or equal to size()");
      cern.jet.random.sampling.RandomSamplingAssistant sampler =
          new cern.jet.random.sampling.RandomSamplingAssistant(n, size(), randomGenerator);
      for (int i = n; --i >= 0; ) {
        if (sampler.sampleNextElement()) buffer.add(this.elements.getQuick(i));
      }
    } else { // with
      cern.jet.random.Uniform uniform = new cern.jet.random.Uniform(randomGenerator);
      int s = size();
      for (int i = n; --i >= 0; ) {
        buffer.add(this.elements.getQuick(uniform.nextIntFromTo(0, s - 1)));
      }
      buffer.flush();
    }
  }
Esempio n. 3
0
 @Override
 public int nextIntBetween(int min, int max) {
   return uniform.nextIntFromTo(min, max);
 }