The way I found the problem on the web:
Give me an algorithm to shuffle a deck of cards, given that the cards are stored in an array of ints.
The way I found the problem on the web:
Give me an algorithm to shuffle a deck of cards, given that the cards are stored in an array of ints.
2 replies on “Shuffle a deck of cards”
Given a deck of cards encoded as an array pick a pivot point to split the deck in half. Then since shuffled cards are shuffled from the bottom of the split deck we shuffle from the end / back of the deck. The mod
operator makes sure we only swap every other card – otherwise it is just swapping the two halves of the card deck.
Here is my version with the wrapping code.