commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Thomas Neidhart (JIRA)" <>
Subject [jira] [Commented] (MATH-942) DiscreteDistribution.sample(int) may throw an exception if first element of singletons of sub-class type
Date Sun, 10 Mar 2013 12:43:12 GMT


Thomas Neidhart commented on MATH-942:

What do you think about the following extension:

     * Generate a random sample from the distribution.
     * <p>
     * If the requested samples fit in the specified array, it is returned
     * therein, with any trailing elements set to null. Otherwise, a new array
     * is allocated with the runtime type of the specified array
     * and the size of this collection.
     * @param sampleSize the number of random values to generate.
     * @param array the array to populate.
     * @return an array representing the random sample.
     * @throws NotStrictlyPositiveException if {@code sampleSize} is not
     * positive.
    public T[] sample(int sampleSize, final T[] array) throws NotStrictlyPositiveException
        if (sampleSize <= 0) {
            throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES,

        T[] out;
        final int arraySize = array != null ? array.length : -1;
        if (arraySize < sampleSize) {
            @SuppressWarnings("unchecked") // safe as both are of type T
            final T[] unchecked = (T[]) Array.newInstance(array.getClass().getComponentType(),
            out = unchecked;
        } else {
            out = array;

        for (int i = 0; i < sampleSize; i++) {
            out[i] = sample();

        return out;


Similar to the toArray(T[]) methods of Collections, a user could already provide an array
which is used if the sample size fits into it, otherwise it is created in a type-safe way,
and the return-type is of T[] so that a user does not have to do a cast.
> DiscreteDistribution.sample(int) may throw an exception if first element of singletons
of sub-class type
> --------------------------------------------------------------------------------------------------------
>                 Key: MATH-942
>                 URL:
>             Project: Commons Math
>          Issue Type: Bug
>            Reporter: Piotr Wydrych
>             Fix For: 3.2
>         Attachments:
> Creating an array with {{Array.newInstance(singletons.get(0).getClass(), sampleSize)}}
in DiscreteDistribution.sample(int) is risky. An exception will be thrown if:
> * {{singleons.get(0)}} is of type T1, an sub-class of T, and
> * {{DiscreteDistribution.sample()}} returns an object which is of type T, but not of
type T1.
> To reproduce:
> {code}
> List<Pair<Object,Double>> list = new ArrayList<Pair<Object, Double>>();
> list.add(new Pair<Object, Double>(new Object() {}, new Double(0)));
> list.add(new Pair<Object, Double>(new Object() {}, new Double(1)));
> new DiscreteDistribution<Object>(list).sample(1);
> {code}
> Attaching a patch.

This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see:

View raw message