Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Randomize and shuffle array of numbers in Java

import java.util.Arrays;
import java.util.Random;
public class Demo {
	public static void main(String[] args) {
		int[] arr = { 20, 40, 60, 80,100, 120, 140, 160, 180, 200};
		System.out.println("The integer array = "+Arrays.toString(arr));
		Random rand = new Random();
		for(int i = 0; i<arr.length; ++i) {
			int index = rand.nextInt(arr.length - i);
			int tmp = arr[arr.length - 1 - i];
			arr[arr.length - 1 - i] = arr[index];
			arr[index] = tmp;
		}
		System.out.println("Shuffle integer array = "+Arrays.toString(arr));
	}
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.