How to make an array randomly disordered, that is, disordered order?

Posted by woolyg on Sat, 30 Nov 2019 11:53:07 +0100

How to make array elements out of order?
At the beginning, I was a little confused. I just thought of using Random to generate a Random integer rd, and then let the array elements move rd positions forward. However, this method is not really disordered arrangement. Obviously, we can see the relationship with the arrangement of the original array, which has not achieved the goal.
Obviously, we need to change our thinking and get experience from Apple selection in our daily life. How to randomly pick an apple from the basket, then put it back, and pick an apple again, and the shape of the apple is almost the same, to ensure that the apple selected this time is not the last apple selected, how to do this? We can put a label on the selected apple. Next time we pick another apple, we will consciously not pick the apple with the label. Repeat this many times, and you can randomly pick out each apple.

1. Define an array first, and then use the Arrays tool class to generate the same copy as this array.

	int nums[]={12,14,13,15,18,19,20};									//Original array
	System.out.println("The original array is:"+Arrays.toString(nums));
	int[] copiedNums = Arrays.copyOf(nums,nums.length) ; 	//Replica array

2. Define a tag number that is different from all elements of the array. This number is a key point.

	int countsDifferent=0; 	// Number of times tag number is different from array element
	int different=0;		// Tag number
	Random rd= new Random();	
	outCycle:				//Outer loop label
	/**Generate tag numbers and verify
	***/
	while(true){
		different=rd.nextInt();		// 	Generate a tag number	
		for(int num : copiedNums){
		/**If the tag number is the same as any element in the array,
		Then exit the inner loop and continue to generate new tag numbers
		The next outer loop, i.e. some inner loops
		**/
			if(different == num ){
				break;		
				/**If the tag number is different from any element in the array,
				If the count difference is equal to
				Array length indicates that the tag number is different from all elements of the array.
				At this time, exit the outer loop, that is, the end of the whole double loop. Tag number
				The achieved value has achieved the design purpose.
			**/	
			}else{
				countsDifferent++;
				if(countsDifferent>=copiedNums.length){
					break outCycle;
				}
			} 
		}
	}

3. Assign an element of the random position of the replica array that is not a difference element to an element of the original array, and assign this element in the replica as a difference tag number. Traverse the array according to this rule.

	int index =0;			//Random index of replica array
	for(int i=0 ;i< nums.length; i++)
	{
		while(true)
		{
			/***Generate random index of replica array
			Note that we need a range of 0 to nums.length-1
			The index value of. And Random's nextInt(int bound) method produces
			[0,bound)The random number of the range, excluding the bound value, which is generated by this method
			Is a number in the range 0 to (bound-1).
			Because of the boundary factor, the parameter in the method is num.length instead of num.length-1.
			***/
			index = rd.nextInt(nums.length);	
			if(copiedNums[index] != different)
			{
				nums[i]=copiedNums[index]; //The copy element of random position is assigned to the original array
				/***
				Assign the acquired replica element to the tag number to ensure that the next acquired random location replica element will not 
			   This element.
				**/
				copiedNums[index]= different;		
				/***
				Every element in the original array that gets a new random position of non difference value in the replica array exits the inner layer
				Loop, the next outer loop, the original array elements of the next index.
				**/
				break;	
			}
		}
	}
	System.out.println("Unordered array:"+Arrays.toString(nums));