Exemplo n.º 1
0
 public static void shuffleArray(long[] a) {
   int n = a.length;
   Random random = new Random();
   random.nextInt();
   for (int i = 0; i < n; i++) {
     int change = i + random.nextInt(n - i);
     swap(a, i, change);
   }
 }
Exemplo n.º 2
0
 /** Returns the index of the largest value in the array.
  * In case of a tie, an the index is selected randomly.
  */
 public static int maxIndex(int[] from, Random rand) {
   assert rand != null;
   int result = 0;
   int maxCount = 0; // count of maximal element for a 1 item reservoir sample
   for( int i = 1; i < from.length; ++i ) {
     if( from[i] > from[result] ) {
       result = i;
       maxCount = 1;
     } else if( from[i] == from[result] ) {
       if( rand.nextInt(++maxCount) == 0 ) result = i;
     }
   }
   return result;
 }