Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Lisp
(let ((end (length x)))
 (loop for i from 0 below end
       do (rotatef (aref x i)
                   (aref x (+ i (random (- end i)))))))

This sorts a lisp vector.
#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...