Step by Step write algorithm (shuffle algorithm)

Source: Internet
Author: User
Tags shuffle

Original: Step by step write algorithm (shuffle algorithm)


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


Poker Shuffle is a game we like to play in our life. So do we have any way to design a poker shuffle method? In the C runtime there is a random function rand, which can generate any number 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 so far. Friends are welcome to talk about other methods.

(1) Global Shuffle method

The steps are as follows:

A) first generate an array of size 54, initialized to 1~54

b) According to 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 this:

void Get_rand_number (int array[], int length) {int index;int value;int median;if (NULL = = Array | | 0 = = length) return;/* each time  Assign the data to be exchanged at any time */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 simple, but there is a problem, we found that each shuffle after the original washing cards will be carried out two times, the individual feel a bit unreasonable, so may wish to improve:

A) Similarly, first we generate an array of size 54, with arrays arranged as 1~54

b) The index board starts at 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 this:

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 data already allocated; median = Array[in Dex];array[index] = array[value];array[value] = median;}}

Note: The above two algorithms are not I want to come out, welcome the initial author of the algorithm and I contacted, 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.