Shuffle or Randomize a list in Java

  • Shuffling a list
    Collections.shuffle() is used to shuffle lists in java.

    Class hierarchy:

    javautilCollections
    

    Syntax:



    Collections.shuffle(list);
    

    Examples:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate working of shuffle()
    import java.util.*;
      
    public class GFG {
        public static void main(String[] args)
        {
            ArrayList<String> mylist = new ArrayList<String>();
            mylist.add("ide");
            mylist.add("quiz");
            mylist.add("geeksforgeeks");
            mylist.add("quiz");
            mylist.add("practice");
            mylist.add("qa");
      
            System.out.println("Original List : \n" + mylist);
      
            Collections.shuffle(mylist);
      
            System.out.println("\nShuffled List : \n" + mylist);
        }
    }

    chevron_right

    
    

    Output:

    Original List : 
    [ide, quiz, geeksforgeeks, quiz, practice, qa]
    
    Shuffled List : 
    [ide, practice, quiz, qa, geeksforgeeks, quiz]
    
  • Shuffling a list using user provided Random Obbject
    Syntax:

    Collections.shuffle(list, Random object);
    

    Examples:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate working of shuffle()
    // with user provided source of randomness.
    import java.util.*;
      
    public class GFG {
        public static void main(String[] args)
        {
            ArrayList<String> mylist = new ArrayList<String>();
            mylist.add("ide");
            mylist.add("quiz");
            mylist.add("geeksforgeeks");
            mylist.add("quiz");
            mylist.add("practice");
            mylist.add("qa");
      
            System.out.println("Original List : \n" + mylist);
      
            // Here we use Random() to shuffle given list.
            Collections.shuffle(mylist, new Random());
            System.out.println("\nShuffled List with Random() : \n"
                               + mylist);
      
            // Here we use Random(3) to shuffle given list. Here 3
            // is seed value for Random.
            Collections.shuffle(mylist, new Random(3));
            System.out.println("\nShuffled List with Random(3) : \n"
                               + mylist);
      
            // Here we use Random(5) to shuffle given list.  Here 5
            // is seed value for Random.
            Collections.shuffle(mylist, new Random(5));
            System.out.println("\nShuffled List with Random(5) : \n"
                               + mylist);
        }
    }

    chevron_right

    
    

    Output:

    Original List : 
    [ide, quiz, geeksforgeeks, quiz, practice, qa]
    
    Shuffled List with Random() : 
    [geeksforgeeks, practice, qa, ide, quiz, quiz]
    
    Shuffled List with Random(3) : 
    [quiz, ide, practice, quiz, geeksforgeeks, qa]
    
    Shuffled List with Random(5) : 
    [ide, quiz, geeksforgeeks, quiz, practice, qa]
    
  • How to write our own Shuffle method?
    We can use Fisher–Yates shuffle Algorithm that works in O(n) time.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to shuffle a given array
    import java.util.Random;
    import java.util.Arrays;
    public class ShuffleRand 
    {
        // A Function to generate a random permutation of arr[]
        static void randomize( int arr[], int n)
        {
            // Creating a object for Random class
            Random r = new Random();
               
            // Start from the last element and swap one by one. We don't
            // need to run for the first element that's why i > 0
            for (int i = n-1; i > 0; i--) {
                   
                // Pick a random index from 0 to i
                int j = r.nextInt(i);
                   
                // Swap arr[i] with the element at random index
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
      
            // Prints the random array
            System.out.println(Arrays.toString(arr));
        }
           
        // Driver Program to test above function
        public static void main(String[] args) 
        {         
             int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
             int n = arr.length;
             randomize (arr, n);
        }
    }

    chevron_right

    
    

    Output:

    [5, 7, 1, 3, 6, 8, 4, 2]
    


My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.