"Disclaimer: All rights reserved, welcome reprint. Not for commercial use.
Contact us: feixiaoxing @163.com "
Poker Shuffle is our life more like playing games.
Do we have any way to design a poker shuffle method? In the C execution Library there is a random function rand, which can generate random numbers between 0~32767. So is it possible to use such a function to shuffle our poker cards?
Here I'll give you a little talk about the two algorithms that I've seen right now. Friends are welcome to talk about other methods.
(1) Global Shuffle method
The process is seen as follows:
A) first generate an array of size 54, initialized to 1~54
b) According to index 1 to 54. Gradually shuffle each index board, first generate a remainder value = rand% 54, then our index card and this remainder card exchange processing
c) such as multi-index to 54 after the end. A deck of cards will be washed.
The code looks like the following:
void Get_rand_number (int array[], int length) {int index;int value;int median;if (NULL = = Array | | 0 = = length) return;/* each time Randomly allocate the data to be exchanged when licensing */for (index = 0; index < length; index + +) {value = rand ()% Length;median = Array[index];array[index] = Array[value];array[value] = median;}}
(2) Local Shuffle method
The above algorithm is very easy, but there is a problem, we found that each shuffle after the original washing cards will be carried out two times, the individual think a little to say just go, so it is best to improve:
A) The same, first we generate an array of size 54, the array is arranged as 1~54
b) the index plate starts from 1. Ended 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) When all the index cards are washed, a deck of cards is ready.
The code looks like the following:
void Get_rand_number (int array[], int length) {int index;int value;int median;if (NULL = = Array | | 0 = = length) return;/* Licensing */for (index = 0; index < length; index + +) {value = index + rand ()% (Length-index) for the assigned data; median = Array[in Dex];array[index] = array[value];array[value] = median;}}
Note: Neither of these algorithms is what I think. Welcome algorithm initial contact with my author, I will add signs in the article.
Step by Step write algorithm (shuffle algorithm)