Creer un patch nth random en loop sans repetition

Je voudrais faire un patch avec nth random avec un nombre de tirages optionel choisi par l’utilisateur mais qui donnerai des listes dont il n’y aurait pas deux fois de suite le meme tirage afin d’eviter des retitions consecutives .


Hi Patrick.

One way would be to compare the ‘current’ value from nth-random with the previous drawn value, within a recursive call or iteration step, and select what to do based on that comparison.

If you accumulate the values you get from nth-random in a list, using e.g. ‘cons’, you can check (using ‘omif’) whether the current value 'equal’s the previous drawn value (using e.g. ‘car’). And if this is the case ignore it, and go on for another recursion (or iteration).

Here’s some lisp code:

(defun x-nth-random (mylist n out)
  (if (zerop n) ; recursion control
      (let ((this (nth-random mylist))) ; next value
        (if (equal this (car out))	; is equal?
            (x-nth-random mylist n out)
            (x-nth-random mylist (- n 1) (cons this out))))))

Thank you for your answer i will try it