SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
Chapter:
Java Primer
Exercise:
Exercises
Question:21 | ISBN:9781118771334 | Edition: 6

Question

Write a Java method that takes an array containing the set of all integers in the range 1 to 52 and shuffles it into random order. Your method should output each possible order with equal probability.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

package java_problems_datastructures;

import java.util.Random;
import java.util.Arrays;

// We can achieve the shuffle by either using shuffle method in Collections or using Random class
public class Shuffle {

	static int[] printRandomArray(int array[], int arraySize) {
		// Lets solve it using Random class
		Random random = new Random();

		for (int i = 1; i < arraySize - 1; i++) {

			int j = random.nextInt(i);

			// Swap arr[i] with the element at random index
			int tempArray = array[i];
			array[i] = array[j];
			array[j] = tempArray;
		}
		return array;
		

	}

	
	public static void main(String[] args) {
		int[] pack = new int[52];
		for (int i = 0; i < 52; i++) {
			pack[i] = i + 1;
		}
		int size = pack.length;
		
		// each time we call the method it prints the random values in range 1-52 with equal probability
		// calling it 4 times to make sure it prints each time uniquely
		System.out.println(Arrays.toString(printRandomArray(pack, size)));
		System.out.println(Arrays.toString(printRandomArray(pack, size)));
		System.out.println(Arrays.toString(printRandomArray(pack, size)));
		System.out.println(Arrays.toString(printRandomArray(pack, size)));
	}
}

Output:

[17, 29, 8, 26, 42, 10, 21, 50, 5, 40, 46, 4, 39, 2, 28, 20, 32, 38, 48, 49, 36, 15, 1, 47, 7, 25, 35, 45, 33, 24, 51, 12, 43, 31, 9, 14, 16, 44, 18, 37, 19, 30, 11, 41, 23, 34, 13, 6, 22, 27, 3, 52]
[39, 47, 23, 51, 32, 41, 15, 44, 20, 37, 19, 21, 43, 16, 2, 40, 7, 25, 33, 31, 11, 28, 13, 14, 48, 36, 18, 27, 12, 38, 3, 46, 35, 17, 4, 1, 6, 9, 42, 45, 24, 26, 30, 22, 49, 8, 5, 10, 29, 34, 50, 52]
[51, 34, 18, 2, 43, 33, 26, 40, 45, 29, 13, 4, 9, 20, 46, 47, 44, 3, 23, 50, 27, 10, 42, 6, 15, 16, 35, 38, 39, 5, 7, 48, 31, 11, 12, 41, 24, 36, 8, 1, 37, 14, 19, 30, 17, 25, 32, 22, 28, 49, 21, 52]
[16, 24, 50, 20, 36, 43, 34, 41, 42, 38, 18, 9, 15, 1, 7, 32, 10, 47, 28, 8, 26, 6, 33, 12, 21, 29, 3, 13, 14, 2, 30, 46, 22, 23, 25, 31, 11, 19, 35, 40, 5, 51, 49, 45, 39, 27, 4, 44, 48, 17, 37, 52]

 

0 0

Discussions

Post the discussion to improve the above solution.