code: public static int[] randomSubset(int k, int n) {
          assert(0 < k && k <= n);
          Random r = new Random();
          int t = 0, m = 0;
          int[] result = new int[k];
  
          while (m < k) {
              double u = r.nextDouble();
              if ( (n - t) * u < k - m ) {
                  result[m] = t;
                  m++;
              }
              t++;
          }
          return result;
      }
docs: Get a random sample of k out of n elements.
  
  See Algorithm S, D. E. Knuth, The Art of Computer Programming, Vol. 2, p.142.