SHARE
TWEET

Untitled

a guest Oct 22nd, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (defun pick (index bag)
  2.   (let* ((range (car bag))
  3.          (beg (car range))
  4.          (end (cdr range))
  5.          (last (- end beg)))
  6.     (if (<= index last)
  7.         (let ((n (+ beg index)))
  8.           (cons n
  9.                 (cond
  10.                  ((= last 0)
  11.                   (rest bag))
  12.                  ((= index 0)
  13.                   (cons `(,(1+ beg) . ,end) (rest bag)))
  14.                  ((= index last)
  15.                   (cons `(,beg . ,(- end 1)) (rest bag)))
  16.                  (t
  17.                   (concatenate 'list
  18.                                `((,beg . ,(- n 1))
  19.                                  (,(1+ n) . ,end))
  20.                                (rest bag))))))
  21.       (let* ((rec     (pick (- index last 1) (rest bag)))
  22.              (n       (car rec))
  23.              (new-bag (cdr rec)))
  24.         (cons n (cons range new-bag))))))
  25.  
  26. (defun generate (count limit)
  27.   (let ((bag `((1 . ,limit)))
  28.         (result nil)
  29.         n pick-result)
  30.     (dotimes (i count)
  31.       (setq pick-result (pick (random (- limit i)) bag))
  32.       (setq n   (car pick-result))
  33.       (setq bag (cdr pick-result))
  34.       (setq result (cons n result)))
  35.     result))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top