Hello everyone, this is my first help post on daniweb so please excuse me if I have done something wrong. So as for my question, I have to shuffle an array of card objects. This is the code I have come up with so far.

public void shuffleDeck(){
    Random seed = new Random(123456);
    int n = deck.length;
    int N = n - 1;
    for(int i = 0; i < N; i++){
        int k = seed.nextInt(deck.length);
        Card swap = deck[k];
        deck[N] = deck[k];
        deck[k] = deck[N];
        N--;
    }
}

When done correctly the output is supposed to be 3d, Ah, 5d, Js, Td, Qs, 6c, 4d, Kc, and 9h as the top 10 cards when seeding in 123456. However, the output I receive is

Ace of Clubs
Two of Clubs
Three of Clubs
Four of Clubs
Five of Clubs
Six of Clubs
Seven of Clubs
Eight of Clubs
Nine of Clubs
Ten of Clubs
Jack of Clubs
Queen of Clubs
King of Clubs
Ace of Diamonds
Two of Diamonds
Three of Diamonds
Four of Diamonds
Five of Diamonds
Six of Diamonds
Seven of Diamonds
Eight of Diamonds
Nine of Diamonds
Ten of Diamonds
Jack of Diamonds
Queen of Diamonds
King of Diamonds
Nine of Diamonds
Eight of Diamonds
Ace of Hearts
Six of Clubs
King of Clubs
Three of Spades
Two of Hearts
Four of Hearts
Eight of Diamonds
Queen of Diamonds
Three of Spades
Five of Clubs
Five of Clubs
Two of Hearts
Eight of Diamonds
Eight of Clubs
Eight of Diamonds
Eight of Diamonds
Seven of Hearts
Three of Spades
Three of Diamonds
Six of Diamonds
Nine of Diamonds
Jack of Diamonds
Ace of Spades
Three of Diamonds

I would really appreciate if someone could tell me what I am doing wrong? Thank you.
p.s I believe there is a .shuffle() method but we are not allowed to use that I believe.

Edited 4 Years Ago by tingwong

Card swap = deck[k];
deck[N] = deck[k];
deck[k] = deck[N];

Here's a problem. You don't use "swap", you just put the original entry back.

Is this the right way to do it?

public void Shuffle(){
        Random seed = new Random(123456);
        int n = deck.length;
        for(int N = n - 1; N > -1; N--){
            int k = seed.nextInt(n);
            Card swap = deck[k];
            deck[k] = deck[N];
            deck[N] = swap;
        }
    }

Because I have tried this and it is still does not show the correct output.

sir, can i use fisher-yates for providing random set of questions? from 50 questions it would randomly provide 20 questions?

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