We would like to know how to randomize and Shuffle Array of numbers or strings.
import java.util.Arrays; import java.util.Random; /* w w w . j a v a 2 s . c o m*/ public class Main { public static void main(String[] args) { int[] nums = new int[100]; for(int i = 0; i < nums.length; ++i) { nums[i] = i + 1; } Random generator = new Random(); for(int i = 0; i < nums.length; ++i) { int arrayIndex = generator.nextInt(nums.length - i); int tmp = nums[nums.length - 1 - i]; nums[nums.length - 1 - i] = nums[arrayIndex]; nums[arrayIndex] = tmp; } System.out.println(Arrays.toString(nums)); } }
The code above generates the following result.