Here you can find the source of shuffle(int a[])
Parameter | Description |
---|---|
a | the array |
public static void shuffle(int a[])
//package com.java2s; //License from project: Open Source License import java.util.Random; public class Main { /** A random number generator. */ public static Random random = new Random(System.currentTimeMillis()); /**/*from ww w .ja va 2s. c o m*/ * Shuffles (randomizes) the contents of the given array. * * @param a * the array */ public static void shuffle(int a[]) { for (int i = 0; i < a.length - 1; i++) { int tmp = a[i]; int index = i + 1 + random.nextInt(a.length - i - 1); a[i] = a[index]; a[index] = tmp; } } /** * Shuffles (randomizes) the contents of the given array. * * @param a * the array */ public static void shuffle(Object a[]) { for (int i = 0; i < a.length - 1; i++) { Object tmp = a[i]; int index = i + 1 + random.nextInt(a.length - i - 1); a[i] = a[index]; a[index] = tmp; } } }