Shuffle Shuffle algorithm

Source: Internet
Author: User
Tags shuffle shuffle shuffle

1.fisher–yates Shuffle (Faysheye random scrambling algorithm)

The idea of the algorithm is to randomly extract a new number from the original array into the new array. The algorithm is described in English as follows:

    • Write down the numbers from 1 through N.
    • Pick a random number k between one and the number of Unstruck numbers remaining (inclusive).
    • Counting from the low end, strike out the kth number is not yet struck out, and write it down elsewhere.
    • Repeat from step 2 until all the numbers has been struck out.
    • The sequence of numbers written down in step 3 are now a random permutation of the original numbers.

Algorithm Description:
Let X1, X2 .... XN is the set of N numbers to be shuffled.

    1. Set J to N
    2. Generate a random number R. (uniformly distributed between 0 and 1)
    3. Set K to (jr+1). K is now a random integer, between 1 and J.
    4. Exchange Xk and Xj
    5. Decrease J by 1.
    6. If J > 1, Return to step 2.

The Golang code is implemented as follows:

package shuffleimport (   "fmt")func FisherYatesShuffle(array []int) []int {   newArray := make([]int, 0)   for {   Loop:       length := len(array)       if length == 0 {           break       }       for i := 0; i <= length; i++ {           p := RandInt64(0, int64(length))           fmt.Printf("每次生成的随机数:%d\n", p)           newArray = append(newArray, array[p])           array = append(array[0:p], array[p+1:]...)           goto Loop       }   }   return newArray}package shuffleimport (   "fmt"   "testing")func TestFisherYatesShuffle(t *testing.T) {   array := []int{1, 2, 3, 4, 5}   shuffle := FisherYatesShuffle(array)   fmt.Println(shuffle)}

Operation Result:

每次生成的随机数:0生成的新数组:[1]每次生成的随机数:3生成的新数组:[1 5]每次生成的随机数:0生成的新数组:[1 5 2]每次生成的随机数:1生成的新数组:[1 5 2 4]每次生成的随机数:0生成的新数组:[1 5 2 4 3]

2. Knuth-durstenfeld Shuffle

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 (N2) to O (n).

The Golang code is implemented as follows:

package shuffleimport (   "fmt"   "math/rand")func KnuthDurstenfeldShuffle(array []int) []int {   for i := len(array) - 1; i >= 0; i-- {       p := RandInt64(0, int64(i))       fmt.Printf("每次生成的随机数:%d\n", p)       a := array[i]       array[i] = array[p]       array[p] = a       fmt.Println("置换后的数组为:", array)   }   return array}// RandInt64 区间随机数func RandInt64(min, max int64) int64 {   if min >= max || max == 0 {       return max   }   return rand.Int63n(max-min) + min}package shuffleimport (   "fmt"   "testing")func TestKnuthDurstenfeldShuffle(t *testing.T) {   array := []int{1, 2, 3, 4, 5}   shuffle := KnuthDurstenfeldShuffle(array)   fmt.Println(shuffle)}

Operation Result:

每次生成的随机数:2置换后的数组为: [1 2 5 4 3]每次生成的随机数:1置换后的数组为: [1 4 5 2 3]每次生成的随机数:1置换后的数组为: [1 5 4 2 3]每次生成的随机数:0置换后的数组为: [5 1 4 2 3]每次生成的随机数:0置换后的数组为: [5 1 4 2 3][5 1 4 2 3]

From the running results you can see that as the algorithm runs, the range of random numbers available for selection is decreasing, while the elements in the array tend to be unordered.

Potential deviations:
In the implementation of the Fisher-yates Faysheye random scrambling algorithm, there may be deviations, although this deviation is very not obvious. Reasons: First, the implementation of the algorithm itself problems, and the second is the algorithm based on the random number generator.

    • 1. The realization of each permutation of non-equal probability appears
      The selection of J in the algorithm flow is from 0...i-1, so the fisher-yates algorithm becomes the Sattolo algorithm, in common (n-1)! Different permutations, not n! species.
      J is selected within the range of all 0...N, some sequences must be arranged by n^n to be generated.
    • 2. The random number generator used by the Fisher-yates Faysheye algorithm is a PRNG pseudo-random number generator

The sequence generated by such a pseudo-random number generator is determined entirely by the internal state of the start of the sequence, and the different scrambling generated by such a pseudo-random generator can not be more than the number of different states of the generator, even when the number of possible states exceeds the permutation, An unhealthy mapping from the number of States to an arrangement makes some permutations appear more frequently than others. So the number of States needs to be several levels higher than the number of permutations.

Many language or library functions have built-in pseudo-random number generators with only 32-bit internal states, which means that you can generate 2^32 of different sequence numbers. If such a random device for scrambling a pair of 52 cards, can only produce 52! = A small fraction of the possible permutations of the 2^225.6 species. For random number generators with less than 226 bits of internal state, it is not possible to produce all permutations of 52 cards.

The relationship between the number of internal states of the pseudo-random number generator and the maximum linear table lengths that can be generated based on each permutation of this generator:

Shuffle.jpg

3.inside-out algorithm

Knuth-durstenfeld Shuffle is a in-place algorithm, the raw data is directly disrupted, some applications may need to retain the original data, so you need to open up a new array to store the scrambled sequence. The basic idea of the inside-out algorithm algorithm is to have a cursor I scan a copy of the original data from the back, a random subscript J between [0, I], then replace the number of position I with the element of position J, and replace the element of the copy data position J with the element of the original data position I. The function is equivalent to exchanging the values at the position of I and J in the copied data.

Related Article

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.