Uploaded image for project: 'Commons RNG'
  1. Commons RNG
  2. RNG-184

ArraySampler to shuffle all primitive array types and generic T[] arrays

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Resolved
    • Major
    • Resolution: Implemented
    • 1.5
    • 1.6
    • sampling
    • None
    • Easy

    Description

      The sampling module contains code to shuffle int[] in the PermutationSampler:

      public static void shuffle(UniformRandomProvider rng, int[] list)
      public static void shuffle(UniformRandomProvider rng,
         int[] list,
         int start,
         boolean towardHead) 

      Shuffling support should be expanded to all primitive types and a generic array type.

      The non-integer domain is out of scope for the PermutationSampler. The shuffle method is present as a utility for permuting arrays of indices.

      I suggest a new API in ArraySampler with a shuffle that can handle sub-ranges as:

      public static void shuffle(UniformRandomProvider rng, int[] data);
      public static void shuffle(UniformRandomProvider rng, int[] data,
                                 int from, int to);
      

      Note that there is a ListSampler that offers similar functionality for a List:

      public static <T> void shuffle(UniformRandomProvider rng,
                                     List<T> list)
      public static <T> void shuffle(UniformRandomProvider rng,
                                     List<T> list,
                                     int start,
                                     boolean towardHead)
      
      // Also sampling
      public static <T> List<T> sample(UniformRandomProvider rng,
                                       List<T> collection,
                                       int k) 
      

      I do not think supporting a range for shuffling here is required as this can be achieved using:

      int from = ...
      int to = ...
      ListSampler.shuffle(rng, list.subList(from, to));
      

      This is the how the half-shuffle method (towards head/tail) is implemented. The origin of the half-shuffle API is unknown but it is not as flexible a sub-range shuffle and I do not propose to implement it for all array types.

      Note that consistency between Lists and arrays would require a sampling method for all arrays, e.g.:

      public static double[] sample(UniformRandomProvider rng,
          double[] array,
          int k) {
      public static <T> T[] sample(UniformRandomProvider rng,
          T[] array,
          int k)
      

      Since this static method uses a new PermutationSampler per sample the method is not suitable for repeat invocation. Development of a sampling API for arrays should be under another ticket, e.g.

      // Sampler returns k elements from the given array
      SharedStateObjectSampler<double[]> createSampler(UniformRandomProvider rng,
                                                       double[] array, int k).

      Such a sampler would internally maintain the PermutationSampler between invocations.

      This ticket will only cover shuffling support in ArraySampler.

      Attachments

        Issue Links

          Activity

            People

              aherbert Alex Herbert
              aherbert Alex Herbert
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: