Step by Step write algorithm (shuffle algorithm)

Source: Internet
Author: User
Tags shuffle


"Disclaimer: Copyright All, welcome reprint, do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


Poker Shuffle is a game we like to play in our lives. So 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) In accordance with the 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 board 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) 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: The above two algorithms are not I want to come out, welcome to the algorithm's initial author and I contact, I will add tags in the article description.



Step by Step write 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.