One-step write algorithm (shuffling algorithm)

Source: Internet
Author: User

 

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

 

 

 

Playing card shuffling is a favorite game in our life. Is there any way for us to design a card shuffling method? There is a random function rand in the c Runtime Library, which can generate 0 ~ Any number between 32767. Is it possible to use such a function to immediately shuffles playing cards?

 

Here I will talk about the two algorithms that I have seen. You are welcome to talk about other methods.

 

(1) Global shuffling

 

The procedure is as follows:

 

A) first generate an array with the size of 54 and the initial value of 1 ~ 54

 

B) shuffles each index card according to the index 1 to 54. First, a remainder value = rand % 54 is generated. Then our index card is exchanged with this remainder card.

 

C) After Multiple indexes reach 54, a deck will be ready.

 

The Code is as follows:

 

 

Void get_rand_number (int array [], int length)

{

Int index;

Int value;

Int median;

 

If (NULL = array | 0 = length)

Return;

 

/* Allocate any data to be exchanged during each licensing */

For (index = 0; index <length; index ++ ){

Value = rand () % length;

 

Median = array [index];

Array [index] = array [value];

Array [value] = median;

}

}

Void get_rand_number (int array [], int length)

{

Int index;

Int value;

Int median;

 

If (NULL = array | 0 = length)

Return;

 

/* Allocate any data to be exchanged during each licensing */

For (index = 0; index <length; index ++ ){

Value = rand () % length;

 

Median = array [index];

Array [index] = array [value];

Array [value] = median;

}

}

 

 

 

 

(2) Local shuffling

The above algorithm is very simple, but there is a problem. We find that the cards we washed will be operated twice after each shuffling. I personally think it is a bit difficult to say, so we may wish to improve it:

 

A) Similarly, we first generate an array of 54 in size, which is arranged as 1 ~ 54

 

B) The index starts from 1 and ends at 54. This time the index card is only exchanged with the remaining cards that have not been washed, value = index + rand () % (54-index)

 

C) after all the index cards are washed, a deck will be ready.

 

The Code is as follows:

 

 

Void get_rand_number (int array [], int length)

{

Int index;

Int value;

Int median;

If (NULL = array | 0 = length)

Return;

/* Do not modify the allocated data during licensing */

For (index = 0; index <length; index ++ ){

Value = index + rand () % (length-index );

Median = array [index];

Array [index] = array [value];

Array [value] = median;

}

}

Void get_rand_number (int array [], int length)

{

Int index;

Int value;

Int median;

If (NULL = array | 0 = length)

Return;

/* Do not modify the allocated data during licensing */

For (index = 0; index <length; index ++ ){

Value = index + rand () % (length-index );

Median = array [index];

Array [index] = array [value];

Array [value] = median;

}

}

 

 

Note: I did not come up with the above two algorithms. You are welcome to contact me as the initial author of the algorithm. I will add the markup instructions in the article.

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.