Skip to main content
added 482 characters in body
Source Link

Great job, everyone. Here's my own answer in C# based on the Fisher-Yates algorithm. Should give you a perfect shuffle if your random number generator is good enough.

English version:

  1. Partially sortRepeatedly swap the deck untilcard at deck[0] with the one at deck[v], where v is the face value of the card at deck[0]. Repeat until v == 0. Since you knowThis will partially sort the deck, but that card's original valuedoesn't matter. You now know Card 0 is at the front of the deck, which means you can steal that space in the array and use it as a loop counter and overwrite it as much as you like. This is the key "cheat" for the problem of local variables.
  2. For eachStarting at position i from 1 to 51(the second card in the deck), swap the card at i with the one at rand(i, 51). Note that you need (note:rand(i, 51), NOT rand(1, 51). That won't give you a random shuffle)ensure that each card is randomized.
  3. Set deck[0] back to zero and0. Now the whole deck is shuffled except for the first card, so swap itdeck[0] with a random carddeck[rand(0, 51)] and you're done.

Should be a perfectly random shuffle.C# version:

public static void shuffle(int[] deck)
{
    while (deck[0] > 0)
        swap(ref deck[0], ref deck[deck[0]]);
    
    for (deck[0] = 1; deck[0] < 52; deck[0]++)
        swap(ref deck[deck[0]], ref deck[rand(deck[0], 51)]);

    deck[0] = 0;
    swap(ref deck[0], ref deck[rand(0, 51)]);
}
public static void shuffle(int[] deck)
{
    while (deck[0] > 0)
        swap(ref deck[0], ref deck[deck[0]]);
    
    for (deck[0] = 1; deck[0] < 52; deck[0]++)
        swap(ref deck[deck[0]], ref deck[rand(deck[0], 51)]);

    deck[0] = 0;
    swap(ref deck[0], ref deck[rand(0, 51)]);
}
while (deck[0] > 0)
    swap(0, deck[0]);

for (deck[0] = 1; deck[0] < 52; deck[0]++)
    swap(deck[0], rand(deck[0], 52));

deck[0] = 0;
swap(0, rand(0, 52));
while (deck[0] > 0)
    swap(0, deck[0]);

for (deck[0] = 1; deck[0] < 52; deck[0]++)
    swap(deck[0], rand(deck[0], 52));

deck[0] = 0;
swap(0, rand(0, 52));

Great job, everyone. Here's my own answer in C# based on the Fisher-Yates algorithm:

  1. Partially sort the deck until deck[0] == 0. Since you know that card's original value, you can use it as a counter and overwrite it as much as you like.
  2. For each position i from 1 to 51, swap the card at i with the one at rand(i, 51) (note: NOT rand(1, 51). That won't give you a random shuffle).
  3. Set deck[0] back to zero and swap it with a random card.

Should be a perfectly random shuffle.

public static void shuffle(int[] deck)
{
    while (deck[0] > 0)
        swap(ref deck[0], ref deck[deck[0]]);
    
    for (deck[0] = 1; deck[0] < 52; deck[0]++)
        swap(ref deck[deck[0]], ref deck[rand(deck[0], 51)]);

    deck[0] = 0;
    swap(ref deck[0], ref deck[rand(0, 51)]);
}
while (deck[0] > 0)
    swap(0, deck[0]);

for (deck[0] = 1; deck[0] < 52; deck[0]++)
    swap(deck[0], rand(deck[0], 52));

deck[0] = 0;
swap(0, rand(0, 52));

Here's my own answer based on the Fisher-Yates algorithm. Should give you a perfect shuffle if your random number generator is good enough.

English version:

  1. Repeatedly swap the card at deck[0] with the one at deck[v], where v is the face value of the card at deck[0]. Repeat until v == 0. This will partially sort the deck, but that doesn't matter. You now know Card 0 is at the front of the deck, which means you can steal that space in the array and use it as a loop counter. This is the key "cheat" for the problem of local variables.
  2. Starting at position 1 (the second card in the deck), swap the card at i with the one at rand(i, 51). Note that you need rand(i, 51), NOT rand(1, 51). That won't ensure that each card is randomized.
  3. Set deck[0] back to 0. Now the whole deck is shuffled except for the first card, so swap deck[0] with deck[rand(0, 51)] and you're done.

C# version:

public static void shuffle(int[] deck)
{
    while (deck[0] > 0)
        swap(ref deck[0], ref deck[deck[0]]);
    
    for (deck[0] = 1; deck[0] < 52; deck[0]++)
        swap(ref deck[deck[0]], ref deck[rand(deck[0], 51)]);

    deck[0] = 0;
    swap(ref deck[0], ref deck[rand(0, 51)]);
}
while (deck[0] > 0)
    swap(0, deck[0]);

for (deck[0] = 1; deck[0] < 52; deck[0]++)
    swap(deck[0], rand(deck[0], 52));

deck[0] = 0;
swap(0, rand(0, 52));
added 272 characters in body
Source Link

C#

Great job, everyone. Here's my own answer in C# based on the Fisher-Yates algorithm:

  1. Partially sort the deck until deck[0] == 0. Since you know that card's original value, you can use it as a counter and overwrite it as much as you like.
  2. For each position i from 1 to 51, swap the card at i with the one at rand(i, 51) (note: NOT rand(1, 51). That won't give you a random shuffle).
  3. Set deck[0] back to zero and swap it with a random card.

Should be a perfectly random shuffle.

public static void shuffle(int[] deck)
{
    while (deck[0] > 0)
        swap(ref deck[0], ref deck[deck[0]]);
    
    for (deck[0] = 1; deck[0] < 52; deck[0]++)
        swap(ref deck[deck[0]], ref deck[rand(deck[0], 51)]);

    deck[0] = 0;
    swap(ref deck[0], ref deck[rand(0, 51)]);
}

Javascript version:

while (deck[0] > 0)
    swap(0, deck[0]);

for (deck[0] = 1; deck[0] < 52; deck[0]++)
    swap(deck[0], rand(deck[0], 52));

deck[0] = 0;
swap(0, rand(0, 52));

...where swap(a, b) swaps deck[a] with deck[b].

C#

Great job, everyone. Here's my own answer in C# based on the Fisher-Yates algorithm:

  1. Partially sort the deck until deck[0] == 0. Since you know that card's original value, you can use it as a counter and overwrite it as much as you like.
  2. For each position i from 1 to 51, swap the card at i with the one at rand(i, 51) (note: NOT rand(1, 51). That won't give you a random shuffle).
  3. Set deck[0] back to zero and swap it with a random card.

Should be a perfectly random shuffle.

public static void shuffle(int[] deck)
{
    while (deck[0] > 0)
        swap(ref deck[0], ref deck[deck[0]]);
    
    for (deck[0] = 1; deck[0] < 52; deck[0]++)
        swap(ref deck[deck[0]], ref deck[rand(deck[0], 51)]);

    deck[0] = 0;
    swap(ref deck[0], ref deck[rand(0, 51)]);
}

C#

Great job, everyone. Here's my own answer in C# based on the Fisher-Yates algorithm:

  1. Partially sort the deck until deck[0] == 0. Since you know that card's original value, you can use it as a counter and overwrite it as much as you like.
  2. For each position i from 1 to 51, swap the card at i with the one at rand(i, 51) (note: NOT rand(1, 51). That won't give you a random shuffle).
  3. Set deck[0] back to zero and swap it with a random card.

Should be a perfectly random shuffle.

public static void shuffle(int[] deck)
{
    while (deck[0] > 0)
        swap(ref deck[0], ref deck[deck[0]]);
    
    for (deck[0] = 1; deck[0] < 52; deck[0]++)
        swap(ref deck[deck[0]], ref deck[rand(deck[0], 51)]);

    deck[0] = 0;
    swap(ref deck[0], ref deck[rand(0, 51)]);
}

Javascript version:

while (deck[0] > 0)
    swap(0, deck[0]);

for (deck[0] = 1; deck[0] < 52; deck[0]++)
    swap(deck[0], rand(deck[0], 52));

deck[0] = 0;
swap(0, rand(0, 52));

...where swap(a, b) swaps deck[a] with deck[b].

deleted 58 characters in body
Source Link

C#

Great job, everyone. Here's my own answer in C# based on the Fisher-Yates algorithm (6 lines, for those keeping track).:

The idea is to partially sort the deck until deck[0] == 0. Once you know the value at that position, you can freely use it as a counter as long as you set it back to zero when you're done.

  1. Partially sort the deck until deck[0] == 0. Since you know that card's original value, you can use it as a counter and overwrite it as much as you like.
  2. For each position i from 1 to 51, swap the card at i with the one at rand(i, 51) (note: NOT rand(1, 51). That won't give you a random shuffle).
  3. Set deck[0] back to zero and swap it with a random card.

Then, for each position i from 1 to 51, you swap deck[i] with deck[rand(i, 51)] (not rand(1, 51), that will throw off the randomness). Finally, set deck[0] back to zero and swap it with a random card. Should be a perfectly random shuffle.

public static void shuffle(int[] deck)
{
    while (deck[0] > 0)
        swap(ref deck[0], ref deck[deck[0]]);
    
    for (deck[0] = 1; deck[0] < 52; deck[0]++)
        swap(ref deck[deck[0]], ref deck[rand(deck[0], 51)]);

    deck[0] = 0;
    swap(ref deck[0], ref deck[rand(0, 51)]);
}

C#

Great job, everyone. Here's my own answer in C# based on the Fisher-Yates algorithm (6 lines, for those keeping track).

The idea is to partially sort the deck until deck[0] == 0. Once you know the value at that position, you can freely use it as a counter as long as you set it back to zero when you're done.

Then, for each position i from 1 to 51, you swap deck[i] with deck[rand(i, 51)] (not rand(1, 51), that will throw off the randomness). Finally, set deck[0] back to zero and swap it with a random card. Should be a perfectly random shuffle.

public static void shuffle(int[] deck)
{
    while (deck[0] > 0)
        swap(ref deck[0], ref deck[deck[0]]);
    
    for (deck[0] = 1; deck[0] < 52; deck[0]++)
        swap(ref deck[deck[0]], ref deck[rand(deck[0], 51)]);

    deck[0] = 0;
    swap(ref deck[0], ref deck[rand(0, 51)]);
}

C#

Great job, everyone. Here's my own answer in C# based on the Fisher-Yates algorithm:

  1. Partially sort the deck until deck[0] == 0. Since you know that card's original value, you can use it as a counter and overwrite it as much as you like.
  2. For each position i from 1 to 51, swap the card at i with the one at rand(i, 51) (note: NOT rand(1, 51). That won't give you a random shuffle).
  3. Set deck[0] back to zero and swap it with a random card.

Should be a perfectly random shuffle.

public static void shuffle(int[] deck)
{
    while (deck[0] > 0)
        swap(ref deck[0], ref deck[deck[0]]);
    
    for (deck[0] = 1; deck[0] < 52; deck[0]++)
        swap(ref deck[deck[0]], ref deck[rand(deck[0], 51)]);

    deck[0] = 0;
    swap(ref deck[0], ref deck[rand(0, 51)]);
}
edited body
Source Link
Loading
Source Link
Loading