Uploaded image for project: 'Commons Math'
  1. Commons Math
  2. MATH-942

DiscreteDistribution.sample(int) may throw an exception if first element of singletons of sub-class type

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 3.2
    • None
    • None

    Description

      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:

      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);
      

      Attaching a patch.

      Attachments

        1. DiscreteDistribution.java.patch
          1 kB
          Piotr Wydrych

        Activity

          People

            Unassigned Unassigned
            wydrych Piotr Wydrych
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: