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

"shuffle" method broken (in class "o.a.c.m.random.RandomDataGenerator")

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.3
    • Labels:
      None

      Description

      The method does not abide by its contract: elements before the "end" index are included in the shuffle.

      /**
       * Uses a 2-cycle permutation shuffle to randomly re-order the last elements 
       * of list.
       *
       * @param list list to be shuffled
       * @param end element past which shuffling begins
       */
      private void shuffle(int[] list, int end) {
          int target = 0;
          for (int i = list.length - 1; i >= end; i--) {
              if (i == 0) { // XXX "0" should be "end"
                  target = 0; // XXX "0" should be "end"
              } else {
                  // NumberIsTooLargeException cannot occur
                  target = nextInt(0, i); // XXX "0" should be "end"
              }
              int temp = list[target];
              list[target] = list[i];
              list[i] = temp;
          }
      }
      

      I'm going to introduce the above corrections in the new implementation to be located in "MathArrays" (cf. issue MATH-1010).

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                erans Gilles
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: