"Daily algorithm" Shuffle algorithm

Source: Internet
Author: User
Tags shuffle

Shuffle algorithm

Given a sequence of n numbers, an algorithm is designed to randomly disrupt it, ensuring that each number appears in the same probability (i.e., in the n! arrangement, the probability of each permutation appearing the same).

The naïve approach:

Suppose the input is an array of num[length].

Randomly select a number, put it in num[0], and then randomly select the number, if the number has been selected, re-select, until the number is not selected obsolete into the num[1], and so on, until all the numbers are selected, it is clear that this method altogether has n! possible, each probability of occurrence is the same.

However, this approach is inefficient because the selected number of re-election will take a lot of time.

Improved Shuffle algorithm:

Based on the shortcomings of the above algorithm, we make improvements: The selected number will no longer be considered. For example NUM[0...K] is the selected number, then our random only in k+1 to length-1 between.

void MySwap(intint &b){    int tmp = x;    x = y;    y = tmp;}void Shuffle(intint length){    if0return;    for (int index = length-11; --index)    {        MySwap(num[index], num[rand()%(index+1)]);    }}

Explain:

Index is the location of the selected card, rand ()% (index+1) produces a random number from 0 to index, but the selected number of subscript is index+1 to length-1, so you can guarantee that the selected number will not be selected again.

Some small details:

The reason for the habit of using –index instead of index– is that index– needs a temporary variable to hold the index value before it is reduced, and –index, which is directly self-reducing and then returns itself, is more efficient. (Of course, the compiler might actually do some optimizations that make the difference between the two very small, which is just a good programming habit).

Use reverse: rand ()% (index+1) makes it easier to generate a random number between 0 and index, if it is a positive order, it needs to be written as:

    for (intindex0indexlength; ++index)    {        MySwap(num[index], num[index+rand()%(length-index)]);    }

Operations are more, and not concise enough.

One last point: the result of each operation of the above code will be the same, if you want to be different each time, you need to add a seed (using Srand ((int) time (0));

Make a little progress every day, Come on!
('? ') )

I have limited level, such as the content of the article has errors and omissions, please point out the reader, thank you!

"Daily 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.