Randomly rearrange an array

Source: Internet
Author: User

This is our first blog post, and we encountered in the development of the first app is a more interesting algorithm, this sort of an array of random sorting problem is not uncommon, but because it is a beginner, so in the process of exploring this algorithm also tangled for a long time, Of course, the final algorithm is also a reference to listen to the Wind Yin Bo master an article: http://wsjiang.iteye.com/blog/1775341, in this acknowledgement!

Solution to the problem:

1, the false idea is to 2~7 the 6 numbers randomly sorted into an array, here we set min for the range of the minimum value 2,max for the range of the line 7,n for the number of numbers to be taken out of this range to form an array, of course, when n equals the length of the range Len is 6 o'clock, So what you get is to re-order the original 6 numbers again randomly.

2, in order to facilitate the introduction of an initialization array source, and all the numbers just in the range are stored in the array, the initialization of the array source is {2,3,4,5,6,7}.

3, in order to facilitate we also introduced another array of result as the last returned array. Here we know the beginning len=6, then randomly get a random number index (0<=index<=len-1), then we will find in the source array, the element in the index position into the No. 0 bit of the result array, This is the time that Len should be built and replaced with Source[lindex] in the source array Source[len].

4, and so on until the array result is full. The final result is an array that is randomly sorted.

The Java code is as follows:

1  Public classTestrandom {2     /**3 * This method is used to re-randomize all numbers of Min to Max within the set range (Min~max) so that they form a set of random numbers. 4 * 1, first initialize a length (max-min+1) of the array source, the elements of the array according to the number from the largest to the small order of Min~max;5 * 2, each time a random number index,0<=index<= (array length len-1), while Len is reduced by 1,6 * Take the element corresponding to the index position in the source array and put it into the final array result, and replace Source[index] with Source[len]. 7      **/8 9      Public Static int[] Randomarray (intMinintMaxintN) {Ten         intlen = max-min+1;//Len is the number of elements in the range One  A         if(Max < min | | n >Len) { -             return NULL; -         } the  -         //initializes the array to be selected for the given range -         int[] Source =New int[Len]; -          for(inti = min; i < Min+len; i++){ +Source[i-min] =i; -         } +  A         int[] result =New int[n]; atRandom rd =NewRandom (); -         intindex = 0; -          for(inti = 0; i < result.length; i++) { -             //Optional Array 0 to (len-2) random subscript -index = Math.Abs (rd.nextint ()% len--); -             //put a random number into the result set inResult[i] =Source[index]; -             //Replaces the number of random numbers in the selected array with the number of subscripts for the array (len-1) you want to select toSource[index] =Source[len]; +         } -         returnresult; the     } *      Public Static voidMain (string[] args) { $          for(inti = 0; i<=5; i++) {Panax Notoginseng         intResult[]=testrandom.randomarray (2,7,6); -              for(intNum:result) { theSystem.out.print (num+ "\ T"); +             } A System.out.println (); the         } +     } -}

The results of the run can be seen differently for each run.

                        

2016-04-03

BOB

Randomly rearrange an array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.