André Fazendeiro
André Fazendeiro

Reputation: 101

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)

Upvotes: 1

Views: 255

Answers (1)

Svante
Svante

Reputation: 51521

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.

Upvotes: 7

Related Questions