Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Js
for (var i = x.length - 1; i > 0; i--) {
    var j = Math.floor(Math.random() * (i + 1));
    var temp = x[j];
    x[j] = x[i];
    x[i] = temp;
}

Shuffle a list in-place using the Fisher-Yates algorithm.
#include <stdlib.h>
#include <time.h>
srand(time(NULL));
for (int i = 0; i < N-1; ++i)
{
    int j = rand() % (N-i) + i;
    int temp = x[i];
    x[i] = x[j];
    x[j] = temp;
}

Shuffles an array of n ints in-place using Fisher-Yates algorithm.

New implementation...