[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.