Java List Random Item sampleWithReplacement(final Random random, final List data, final int sampleSize)

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

Description

Samples a a given number of items from a list with replacement.

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.

Return

An array list of the given size sampled with replacement from the given data.

Declaration

public static <DataType> ArrayList<DataType> sampleWithReplacement(final Random random,
        final List<? extends DataType> data, final int sampleSize) 

Method Source Code


//package com.java2s;
/*/*from   w w  w . jav a2  s.  co m*/
 * 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.ArrayList;

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.
     *
     * @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.
     * @return
     *      An array list of the given size sampled with replacement from the
     *      given data.
     */
    public static <DataType> ArrayList<DataType> sampleWithReplacement(final Random random,
            final List<? extends DataType> data, final int sampleSize) {
        final ArrayList<DataType> result = new ArrayList<DataType>(sampleSize);
        sampleWithReplacementInto(random, data, sampleSize, result);
        return result;
    }

    /**
     * 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. resampleWithReplacement(final List l, final int n)
  2. sample(List l, Random r)
  3. sample(Random random, List list)
  4. sampleRandomSublist(List list, int sampleSize)
  5. sampleWithoutReplacement(List items, int sampleSize)
  6. sampleWithReplacementInto(final Random random, final List data, final int sampleSize, final Collection result)
  7. softMaxIndex(Random rand, int listSize, double temperature)