Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.
4 added 333 characters in body; added 107 characters in body
source | link

C++

#include <cstdlib>
#include <ctime>
#include <iostream>

int deck[52];

void swap(int a, int b) {
    deck[a] ^= deck[b];
    deck[b] ^= deck[a];
    deck[a] ^= deck[b];
}

int r(int a, int b) {
    return a + (rand() % (b - a + 1));
}

void s(int *deck) {
    swap(deck[1]1, randr(2, 51));
    deck[0] *= 100;

    for(deck[0] += 2; (deck[0] % 100) < 51; deck[0]++) {
        swap(deck[deck[0]deck[0] % 100]100,
          deck[randr(0, 1) ? randr(1, (deck[0] % 100) - 1]1) : randr((deck[0] % 100+1100) + 1, 51)]);
    }
    swap(deck[51]51, randr(1, 50)); 

    deck[0] = (deck[0] - 51) / 100;
    swap(deck[randr(1, 51)], deck[0]0);
}

voidint shufflemain(int *deck)a, char** c)
{
    srand(time(0));

    for (int i = 0; i < 52; i++)
        deck[i] = i;

    s(deck);
    s(deck);

    for (int i = 0; i < 52; i++)
        std::cout << deck[i] << " ";
}

Avoids swapping elements with themselves, so has to call twice to be random.

C++

void s(int *deck) {
    swap(deck[1], rand(2, 51));
    deck[0] *= 100;

    for(deck[0] += 2; (deck[0] % 100) < 51; deck[0]++) {
        swap(deck[deck[0] % 100],
          deck[rand(0, 1) ? rand(1, (deck[0] % 100) - 1]) : rand(deck[0] % 100+1, 51)]);
    }
    swap(deck[51], rand(1, 50)); 

    deck[0] = (deck[0] - 51) / 100;
    swap(deck[rand(1, 51)], deck[0]);
}

void shuffle(int *deck) {
    s(deck);
    s(deck);
}

Avoids swapping elements with themselves, so has to call twice to be random.

C++

#include <cstdlib>
#include <ctime>
#include <iostream>

int deck[52];

void swap(int a, int b) {
    deck[a] ^= deck[b];
    deck[b] ^= deck[a];
    deck[a] ^= deck[b];
}

int r(int a, int b) {
    return a + (rand() % (b - a + 1));
}

void s(int *deck) {
    swap(1, r(2, 51));
    deck[0] *= 100;

    for(deck[0] += 2; (deck[0] % 100) < 51; deck[0]++) {
        swap(deck[0] % 100,
          r(0, 1) ? r(1, (deck[0] % 100) - 1) : r((deck[0] % 100) + 1, 51));
    }
    swap(51, r(1, 50)); 

    deck[0] = (deck[0] - 51) / 100;
    swap(r(1, 51), 0);
}

int main(int a, char** c)
{
    srand(time(0));

    for (int i = 0; i < 52; i++)
        deck[i] = i;

    s(deck);
    s(deck);

    for (int i = 0; i < 52; i++)
        std::cout << deck[i] << " ";
}

Avoids swapping elements with themselves, so has to call twice to be random.

3 since swap is XOR, had to remove possibility of swapping with self
source | link

C++

void shuffles(int *deck) {
    forswap(deck[1], rand(2, 51));
    deck[0] *= 100;

    for(deck[0] += 2; (deck[0] % 100) < 51; deck[0]++) {
        deck[0]++;swap(deck[deck[0] % 100],
        swap(deck[deck[0] % 100]deck[rand(0, deck[rand1) ? rand(1, (deck[0] % 100) - 1]) : rand(deck[0] % 100+1, 51)]);
    }
    swap(deck[51], rand(1, 50)); 

    deck[0] = (deck[0] - 51) / 100;
    swap(deck[rand(1, 51)], deck[0]);
}

void shuffle(int *deck) {
    s(deck);
    shuffles(deck);
}

Not golfedAvoids swapping elements with themselves, so has to call twice to be random.

C++

void shuffle(int *deck) {
    for(deck[0] *= 100; (deck[0] % 100) < 51; ) {
        deck[0]++;
        swap(deck[deck[0] % 100], deck[rand(1, 51)]);
    }

    deck[0] = (deck[0] - 51) / 100;
    swap(deck[rand(1, 51)], deck[0]);
    shuffle(deck);
}

Not golfed.

C++

void s(int *deck) {
    swap(deck[1], rand(2, 51));
    deck[0] *= 100;

    for(deck[0] += 2; (deck[0] % 100) < 51; deck[0]++) {
        swap(deck[deck[0] % 100],
          deck[rand(0, 1) ? rand(1, (deck[0] % 100) - 1]) : rand(deck[0] % 100+1, 51)]);
    }
    swap(deck[51], rand(1, 50)); 

    deck[0] = (deck[0] - 51) / 100;
    swap(deck[rand(1, 51)], deck[0]);
}

void shuffle(int *deck) {
    s(deck);
    s(deck);
}

Avoids swapping elements with themselves, so has to call twice to be random.

2 added 28 characters in body
source | link

C++

void shuffle(int *deck) {
    for(deck[0] *= 100; (deck[0] % 100) < 51; ) {
        deck[0]++;
        swap(deck[deck[0] % 100], deck[rand(1, 51)]);
    }

    deck[0] = (deck[0] - 51) / 100;
    swap(deck[rand(1, 51)], (deck[0] - 51) / 100);
    shuffle(deck);
}

Not golfed.

C++

void shuffle(int *deck) {
    for(deck[0] *= 100; (deck[0] % 100) < 51; ) {
        deck[0]++;
        swap(deck[deck[0] % 100], deck[rand(1, 51)]);
    }

    swap(deck[rand(1, 51)], (deck[0] - 51) / 100);
    shuffle(deck);
}

Not golfed.

C++

void shuffle(int *deck) {
    for(deck[0] *= 100; (deck[0] % 100) < 51; ) {
        deck[0]++;
        swap(deck[deck[0] % 100], deck[rand(1, 51)]);
    }

    deck[0] = (deck[0] - 51) / 100;
    swap(deck[rand(1, 51)], deck[0]);
    shuffle(deck);
}

Not golfed.

1
source | link