/*
  A Monte Carlo method to deterimine if all the permutations computed
  by the method 'shuffle' from the class 'Shuffle' are likely likely.

  Invoke the programs as follows
     java Experiment 11 100
  where 11 is the size of the permutations and 100 is the number of trials.
*/

import java.util.Arrays;

public final class Experiment {

   public static void main (final String[] args) {
      final int n = Integer.parseInt (args[0]);      // permutation size
      final int trials = Integer.parseInt (args[1]); // number of trials
      final int[] a = new int[n];                    // array to permute
      final int index=0;                             // only one position is examined
      final int[] freq = new int[n];                 // frequency of a value in the fixed position

      for (int t=0; t<trials; t++) {
         for (int i=0; i<n; i++) a[i]=i;
         Shuffle.shuffle2 (a);           // test fairness of shuffle
         freq[a[index]]++;
      }

      // The distribution of values in the fixed position of the array.
      for (int i=0; i<n; i++) {
         System.out.printf ("n=%d, trials=%d, index=%3d, value=%3d, count=%5d, freq=%7.3f, theoretical prop=%7.3f%n",
            n, trials, index, i, freq[i], (double)freq[i]/trials, 1.0/n);
      }

   }

}