hey all,

I've been trying to shuffle a deck of cards but my shuffle results in the deck duplicating cards. i've used the the knuth shuffle and I had no luck i've found:

var cards = Enumerable.Range(0, 51);
var shuffledcards = cards.OrderBy(a => Guid.NewGuid());

but I don't know how to implement this to be sorted into another deck.
If someone could point me in the right direction in terms of another shuffle or sorting the above into an array or collection I would be grateful.

Thanks.

By "duplicating". do you mean some cards are still in the same position?

If so, thats is perfectly acceptable, a real worls shuffle will not always result in each card at a different position.

Nah, for example I get 5 Clubs twice something like

...
6C
7D
JS
5C
9H
QH
5C
...

Random r = new Random();
// CardArray is an array of type Card (or List<T>, etc. as long as it's indexable)

for (int i = 51; i > 0; i--) {
    int s = r.Next(i);
    Card temp = CardArray[i];
    CardArray[i] = CardArray[s];
    CardArray[s] = temp;
}

I think I tried that piece of code earlier @Momerath and I still kept on getting duplicates.

This code won't generate duplicates. If you are getting duplicates using this code, then you have duplicates in your starting array. And since I just wrote that, you might have tried something like it, but not it :)

Edited 5 Years Ago by Momerath: n/a

:) ok I gues tried your code and I got this result
KC
QC
JC
10C
9C
8C
7C
6C
8C
4C
3C
2C
AC
KS
QS
JS
10S
KC
8S
7S
6S
5S
4S
10S
5S
3C
JS
QH
7S
AC
9H
JS
JS
4S
6S
JS
10C
2H
QS
QH
5S
JD
JD
AC
JS
7S
5S
QS
JD
QS
QS

my original deck is

static string[] deck = { "AD", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD", "AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH", "AS", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS", "AC", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC" };
using System;

namespace TestBed {
    class TestBed {
        static Random r = new Random();
        static void Main() {
            string[] deck = { "AD", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD", 
                              "AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH", 
                              "AS", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS", 
                              "AC", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC" };

            Display(deck);
            Shuffle(deck);
            Display(deck);

            Console.ReadLine();
        }

        static void Display(String[] array) {
            int lb = 0;
            foreach (String s in array) {
                lb = (lb + 1) % 13;
                if (lb == 0) {
                    Console.WriteLine(s);
                } else {
                    Console.Write("{0} ", s);
                }
            }
            Console.WriteLine();
        }

        static void Shuffle(String[] array) {
            int size = array.Length - 1;

            for (int i = size; i > 0; i--) {
                int s = r.Next(i);

                String temp = array[i];
                array[i] = array[s];
                array[s] = temp;
            }
        }
    }
}

No duplicates and is shuffled. You are doing something different in your code that is messing up the shuffle.

Thanks @Momerath ye I don't know what's wrong with my code. But yours works my bad. @ddanbe saw that post a while back but could quite fit it in with my code.

Thanks for all your help on this.

Case Closed :D

This question has already been answered. Start a new discussion instead.