Click here to Skip to main content
15,439,117 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I am trying to create a Blackjack game in C#, but I am stuck in the part of how to shuffle the deck. I made an array of 52 integers.

Here's the code I have written so far. The code is written within a .cs file (so it is not the main file of the game).

 class Deck

        int[] Cards = new int[52];

        public enum SuitType
        public enum Value
            Two = 2,
            Jack = 10,
            Queen = 10,
            King = 10,
        public void ShuffleCards()

What I have tried:

I've tried running a different variety of for loops without obtaining any sort of success.
Updated 1-Aug-16 21:26pm
Garth J Lancaster 1-Aug-16 21:52pm    
I would start by looking at the Knuth-Fisher-Yates shuffling algorithm - a good article on why is here

The comment by Garth J Lancaster has answered your question. Suggest you start by reading this: How not to shuffle - the Knuth Fisher-Yates algorithm[^]
This gives code examples which can be adapted as required:Knuth shuffle - Rosetta Code[^]
Share this answer
Maciej Los 2-Aug-16 1:42am    
Have you explored the C# Random Class and its methods ? I'd like to see some code where you use that; right now it looks like you have not really done anything but write some Enums.

Let's see how simple it is to get a List of 52 unique numbers in the range #0~51:
// required in addition to the usual directives:
using System.Collections.Generic;
using System.Linq;

private Random rnd;

private List<int> ShuffledDeck;

private void ShuffleButton_Click(object sender, EventArgs e)
    // seed the Random with current current time millisecond value
    rnd = new Random(DateTime.Now.Millisecond);
    ShuffledDeck = new List<int>(52);
    int card;

    // create a sequence 0~51
    List<int> DeckSequence = Enumerable.Range(0, 52).ToList();

    for (int i = 0; i < 52; i++)
        card = DeckSequence[rnd.Next(0, DeckSequence.Count)];
We create an ordered list, select a value from it with a random function, remove that selected value from the ordered list; each time we select the ordered list is getting "shorter."

Note: for a discussion of "seeding" the .NET default pseudo-random number generator: [^]. If you want better "quality" randomness, look into the "System.Security.Cryptography.RandomNumberGenerator" facility in .NET.

Enums: note that while having Enum entries with the same assigned value is "legal" in .NET, you should at least think about the issues discussed here: [^].
Share this answer
I recommend not to "shuffle the cards" but pick them randomly from an array initialized with all "Cards". There is no reason for real "shuffling" in a programm. But if you plan to make something connected to real Money - like online poker/blackjack platform - It's better to make it impossible for potential hackers to "see" the shuffled array. If you need the next card - just pick it randomly from the list of all cards left.
Share this answer
BillWoodruff 2-Aug-16 6:35am     CRLF
"If you need the next card - just pick it randomly from the list of all cards left." And how do you prevent the "hackers" from seeing that list ? Or seeing the contents of the list of cards in the current "hands" that the program will have to maintain to implement the game rules ?
johannesnestler 2-Aug-16 9:34am     CRLF
Hi Bill! You are right - it's just a small part of securing your app - but I think it's not the right place to go into this topics here to deep (could say a lot about it...). Anyway Step 1 is "don't make the next card predictable" - think about a real life game - looking to your opponents cards is one kind of cheating, but knowing which card will be drawn next is another level of cheating - don't you agree? Anyway, I never saw a reason for "shuffling" in the card games I implemented - that was the point I was trying to make. But maybe OP is just doing some homework - if so, your solution is a good fit. Kind regards Johannes

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900