There seems to be a lot of shuffling going around here lately. So I looked here to learn about the Fisher-Yates shuffle etc. But unless my English is not good enough I did not find anything about the way we shuffle a card deck here. Take a deck in your left hand and pick out some cards from the middle of the deck with your right hand and place them in front or behind of the rest. Repeat this a number of times.
So I have a little snippet here who does this trick.
It was a snap to do!
C# and .NET are great!
Card shuffling
using System;
using System.Collections.Generic;
namespace Shuffle
{
class Program
{
static void Main(string[] args)
{
const int cNumberOfShuffles = 5;
List<int> cards = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
// Yeah ok this should be 52 for a complete card deck, but programmers tend to be lazy...
Random rand = new Random();
int LowPick = 0;
int HighPick = 0;
int temp = 0;
for (int i = 0; i < cNumberOfShuffles; i++)
{
LowPick = rand.Next(1, 14); //MSDN says 13 + 1 here
HighPick = rand.Next(1, 14);
// make LowPick always smaller than HighPick
if (HighPick < LowPick)
{
temp = LowPick;
LowPick = HighPick;
HighPick = temp;
}
// Pick some cards and remove them from the deck
List<int> pick = cards.GetRange(LowPick - 1, HighPick - LowPick + 1);
cards.RemoveRange(LowPick - 1, HighPick - LowPick + 1);
// insert them back in front
cards.InsertRange(0, pick);
}
// Show the shuffled deck
for (int i = 0; i < cards.Count; i++)
{
Console.Write(cards[i]);
Console.Write(' ');
}
Console.ReadKey();
}
}
}
ddanbe 2,724 Professional Procrastinator Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.