Question

Let me start by saying sorry for my terrible grammar and spelling.

I have a MVC3 project that creates a Deck and adds 52 cards to it. Then i added a shuffle function that only work´s when I step through the loop in the shuffle method.

In my class Deck i create an Array that holds 52 PlayingCard objects. Deck has a method that copies the Array to a List that is displayed in my View, and it has also a method that shuffles the list.

Not easy to explain but the code only runs one time unless i step through it.

My shuffle method in my Deck model.

 public void Shuffle()
 {
    for (int i = 0; i < 51; i++)
    {
        PlayingCard temp;
        temp = myDeck[i];
        Random rnd = new Random();
        int randomNr = rnd.Next(51);
        myDeck[i] = myDeck[randomNr];
        myDeck[randomNr] = temp;
    }   
}

Output after shuffle, whithout step through:

*4 diamonds 1 hearts 2 hearts 3 hearts 4 hearts 5 hearts 6 hearts 7 hearts 8 hearts 9 hearts 10 hearts 11 hearts 12 hearts 13 hearts 1 spades 2 spades 3 spades 4 spades 5 spades 6 spades 7 spades 8 spades 9 spades 10 spades 11 spades 12 spades 13 spades 1 clubs 2 clubs 3 clubs 4 clubs 5 clubs 6 clubs 7 clubs 8 clubs 9 clubs 10 clubs 11 clubs 12 clubs 13 clubs 1 diamonds 2 diamonds 12 diamonds 3 diamonds 5 diamonds 6 diamonds 7 diamonds 8 diamonds 9 diamonds 10 diamonds 11 diamonds 13 diamonds *

Output after step through 15 times:

2 spades 3 clubs 5 spades 3 diamonds 12 diamonds 13 hearts 11 hearts 1 spades 6 clubs 5 diamonds 1 clubs 12 hearts 7 clubs 13 spades 2 clubs 10 hearts 3 spades 4 spades 3 hearts 6 spades 7 spades 8 spades 9 spades 10 spades 11 spades 12 spades 8 hearts 7 hearts 1 hearts 2 hearts 4 clubs 5 clubs 9 hearts 6 hearts 8 clubs 9 clubs 10 clubs 11 clubs 12 clubs 13 clubs 1 diamonds 2 diamonds 4 hearts 4 diamonds 5 hearts 6 diamonds 7 diamonds 8 diamonds 9 diamonds 10 diamonds 11 diamonds 13 diamonds

Output after stepping through the entire loop:

10 hearts 6 clubs 8 hearts 5 hearts 5 clubs 7 diamonds 5 spades 11 diamonds 12 spades 12 clubs 8 diamonds 5 diamonds 1 clubs 9 spades 10 diamonds 12 hearts 8 spades 9 clubs 13 clubs 6 hearts 1 spades 11 spades 1 hearts 12 diamonds 4 clubs 3 clubs 8 clubs 9 diamonds 7 clubs 2 clubs 3 diamonds 1 diamonds 7 spades 10 spades 2 hearts 6 spades 13 spades 4 spades 2 spades 6 diamonds 4 hearts 2 diamonds 4 diamonds 11 clubs 7 hearts 11 hearts 3 spades 3 hearts 10 clubs 13 hearts 9 hearts 13 diamonds

As shown the loop only runs when i step through it. I tried different loops, made a new project, move the shuffle function to the controller, nothing helps.

Anyone have any ideas how I should solve this?

Was it helpful?

Solution

I have created a sample app around this and moving Random rnd = new Random() to outside the loop gives me the behaviour I'd expect.

The reason it only appeared to shuffle one card, in my opinion, is because the randomly generated value was the same value for the entire loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top