Search code examples
lispcommon-lispsbclclisp

Lisp - How to do Random Permutations


I'm fairly new to Lisp and I was wondering what is the simplest way to create a function that given n returns an array with n elements, with the permutations from 0 to n-1, like for example:

(random-permutations 5)

#(2 4 3 1 0)

Solution

  • Create a vector of length n. Fill it with the consecutive numbers. Shuffle (e. g. Fisher-Yates).

    In order to play with all permutations, you might want to take a look at map-permutations from the library alexandria.