Java List Random Item sampleWithReplacementInto(final Random random, final List data, final int sampleSize, final Collection result)

Here you can find the source of sampleWithReplacementInto(final Random random, final List data, final int sampleSize, final Collection result)

Description

Samples a a given number of items from a list with replacement and puts the samples into the given collection.

License

Open Source License

Parameter

Parameter Description
DataType The type of data in the list.
random The random number generator.
data The list to sample from.
sampleSize The sample size. Must be positive.
result The resulting collection to sample into. All sampled elements will be added to this collection..

Declaration

public static <DataType> void sampleWithReplacementInto(final Random random,
        final List<? extends DataType> data, final int sampleSize, final Collection<? super DataType> result) 

Method Source Code

//package com.java2s;
/*// w  w w.  j  a v  a2s.com
 * File:                DiscreteSamplingUtil.java
 * Authors:             Justin Basilico
 * Company:             Sandia National Laboratories
 * Project:             Cognitive Foundry
 *
 * Copyright June 16, 2010, Sandia Corporation.
 * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
 * license for use of this work by or on behalf of the U.S. Government. Export
 * of this program may require a license from the United States Government.
 * See CopyrightHistory.txt for complete details.
 *
 */

import java.util.Collection;

import java.util.List;
import java.util.Random;

public class Main {
    /**
     * Samples a a given number of items from a list with replacement and puts
     * the samples into the given collection.
     *
     * @param   <DataType>
     *      The type of data in the list.
     * @param   random
     *      The random number generator.
     * @param   data
     *      The list to sample from.
     * @param   sampleSize
     *      The sample size. Must be positive.
     * @param   result
     *      The resulting collection to sample into. All sampled elements will
     *      be added to this collection..
     */
    public static <DataType> void sampleWithReplacementInto(final Random random,
            final List<? extends DataType> data, final int sampleSize, final Collection<? super DataType> result) {
        final int dataSize = data.size();
        for (int i = 0; i < sampleSize; i++) {
            final int randomIndex = random.nextInt(dataSize);
            result.add(data.get(randomIndex));
        }
    }
}

Related

  1. sample(List l, Random r)
  2. sample(Random random, List list)
  3. sampleRandomSublist(List list, int sampleSize)
  4. sampleWithoutReplacement(List items, int sampleSize)
  5. sampleWithReplacement(final Random random, final List data, final int sampleSize)
  6. softMaxIndex(Random rand, int listSize, double temperature)