Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Pascal
var Iter,rr: integer; 
[...]
  for Iter := 0 to high(x) do
    begin
      rr := random(high(x))+1;
      tmp := x[Iter];
      x[Iter] := x[rr];
      x[rr] := tmp;
    end;

Go through each element of the list and swap it with a random element.
#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...