Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Java
import static java.util.Arrays.asList;
import static java.util.Collections.shuffle;
shuffle(asList(x));

Where `x` is an array of Object, and not primitive, data-types.
import java.util.Collections;
Collections.shuffle(x);

Already implemented in standard library.
This method alters the list.
If you need predictable results, see shuffle(List<?>, Random).
#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...