Game common algorithm-Shuffle algorithm

Source: Internet
Author: User
Tags shuffle

Shuffle algorithm is a more common face test.

A pair of poker 54 cards, there are 54! Kind of arrangement. The best shuffling algorithm should be able to generate These 54 probabilities! One of the results

code implementation of shuffling algorithm based on Unity

GitHub links

draw the card shuffleprinciple

This is a completely realistic shuffle logic algorithm.

is to draw the last card of the card randomly inserted into the library, so 54 times to complete the shuffle of poker

Complexity of

Space O (1), Time O (n^2)

Pros and cons

If the library is described as an array, this kind of plug-in shuffle inevitably has to move the elements a lot.

fisher_yates Algorithmprinciple

Take two lists, one is the sequence before shuffling a{1,2....54), a sequence used to put the shuffle b,b initially empty

While a is not empty

randomly take a card from a and add the end of B

Complexity of

Space O (n), Time O (n^2)

Code Implementation
1list<int> list =Newlist<int> (pukes.pukes);//Shuffle before the sequence a2list<int> NewList =Newlist<int> (list. Count);//Shuffle after the sequence B3  for(inti =0; i < pukes.pukes.Length; ++i)4 {5     intRandomindex = Random.range (0, List. Count);6     intr = List[randomindex];//Random Fetch cards7 NewList. ADD (r);8 list. RemoveAt (randomindex);9 }TenPukes. Resetpuke (NewList. ToArray ());//sequence B is the result of shuffling
Pros and cons

The algorithm is clear in principle, but it opens up a list, and deleting elements for list is an unavoidable need to move elements

Take 1/54,1/53,... by random numbers generated 54 times 1/1 can generate this 54 with equal probability ! One of the results

Knuth_durstenfeld Algorithm

Knuth and Durstenfeld improved the algorithm on the basis of Fisher and others. A number is randomly taken out of the data that has never been processed, and the number is placed at the end of the array, that is, the end of the array that holds the processed number. This is an in-place scrambling algorithm, and the algorithm time complexity is increased from the Fisher algorithm's O (n 2) to O (n).

1  for (int1;i>0;---i) 2   {3       int randomindex = Random.range (0, i+1); 4       pukes. Swap (Randomindex, i); 5   }

is the best shuffle algorithm

inside_out Algorithm

This algorithm is used by Random_shuffle in C + + STL

principle

Random a subscript J between [0, I], and replace the number of position I with the element of position J

Take 1/1,1/2,... by random numbers generated 54 times 1/54 can generate this 54 with equal probability ! One of the results

Complexity of

Space O (1), Time O (n)

Code Implementation
1  Public Static voidShuffle (pukes pukes)2   {3       intLen =pukes.pukes.Length;4        for(inti =0; i < Len; ++i)5       {6           intRandomindex = Random.range (0, i +1);7 Pukes. Swap (i, randomindex);8       }9}
Random_shuffle

About the Random_shuffle of C + + STL

Its algorithmic principle and Knuth_durstenfeld are similar

Select an element from all elements to swap with position 1, then choose one from the remaining n-1 elements to position 2, and so on

Reference Links

Wikipedia-fisher–yates Shuffle

Game common algorithm-Shuffle 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.