Algorithm of shuffling algorithm

Source: Internet
Author: User

Shuffle algorithm is our common random problem, in playing games, random sort of often encounter, one of the most common place is the group of volumes. It can be abstracted like this : A random array of all natural numbers within M.

 PackageCom.math;ImportJava.util.Random;/** * @authorSummer **/ Public classShuffle {Static Final int[] A =New int[54]; StaticRandom rnd =NewRandom (); Static{                 for(inti=1;i<55;i++) A[i-1] =i; }        Static voidSwapint[] arr,intIintj) {             intTMP =Arr[i]; Arr[i]=Arr[j]; ARR[J]=tmp; }         Public Static voidShuffle () { for(intI=a.length; i>1; i--) Swap (A, I-1, Rnd.nextint (i)); }         Public Static voidMain (String[]args) {shuffle ();  for(inti:a)        {System.out.println (i); }    }}

Simple implementation, what is the problem everyone to correct.

Algorithm of shuffling algorithm

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.