Crates.io | deque_deck |
lib.rs | deque_deck |
version | 0.2.1 |
source | src |
created_at | 2022-09-13 00:36:57.159276 |
updated_at | 2022-09-25 16:25:59.683612 |
description | A simple library for manipulating and shuffling a deck of cards. |
homepage | |
repository | https://github.com/SymmetricChaos/deque_deck |
max_upload_size | |
id | 664062 |
size | 25,387 |
This crate provides a simple interface for creating and manipulating a fully generic deck of cards.
Add this to your Cargo.toml
:
[dependencies]
deque_deck = "0.1.1"
Standard manipulations of a deque data structure are available but for convenience have been renamed to reflect the language used for decks of cards. So "draw" is used in place of "pop" and "cut" is used in place of "left_rotate" and so on.
Various shuffling algorithms are supported. If quality of randomization is needed only the shuffle() methods should be used. All other methods exist either to simulate physical methods of shuffling or are just of interest mathematically. To achieve randomization simulated shuffles need to be called several times.
The default shuffling method uses the Fisher-Yates algorithm which gives every card an equal probabilty of ending up at any position.
let mut deck = Deck::<i32>::from_iter(0..10);
deck.shuffle();
// Deck{ [1, 9, 7, 6, 2, 8, 4, 5, 0, 3] }
The riffle method uses the Gibert-Shannon-Reeds algorithm to pick a point near the middle of the deck (using a binomial distribution), then split the deck and riffle the two halves together.
let mut deck = Deck::<i32>::from_iter(0..10);
deck.riffle();
// Deck{ [5, 6, 0, 7, 1, 8, 2, 3, 4, 9] }
The overhand method uses Pemantle’s algorithm to take packets of cards from the top and place them successively on the bottom. The probability of a card being the end of the next packet is given by the argument n.
let mut deck = Deck::<i32>::from_iter(0..10);
deck.overhand(0.4);
// Deck{ [9, 7, 8, 6, 3, 4, 5, 1, 2, 0] }
Deals out the cards on the deck into n piles amd then stacks those piles in a random order. Although popular this provides extremely poor randomization.
let mut deck = Deck::<i32>::from_iter(0..10);
deck.pile_shuffe(3);
// Deck{ [8, 5, 2, 7, 4, 1, 9, 6, 3, 0] }
Deals off the top n cards of the deck then performs a riffle shuffle with the two decks.
let mut deck = Deck::<i32>::from_iter(0..10);
deck.gilbreath(5);
// Deck{ [5, 6, 4, 7, 8, 3, 2, 9, 1, 0] }
The inverse riffle shuffle and the original Premantle shuffle are available for statistical puroposes but do not represent any real-world shuffling technique. The faro is also provided as, althrough it is not a true shuffle, it is closely related to the riffle shuffle.