Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defun pick (index bag)
- (let* ((range (car bag))
- (beg (car range))
- (end (cdr range))
- (last (- end beg)))
- (if (<= index last)
- (let ((n (+ beg index)))
- (cons n
- (cond
- ((= last 0)
- (rest bag))
- ((= index 0)
- (cons `(,(1+ beg) . ,end) (rest bag)))
- ((= index last)
- (cons `(,beg . ,(- end 1)) (rest bag)))
- (t
- (concatenate 'list
- `((,beg . ,(- n 1))
- (,(1+ n) . ,end))
- (rest bag))))))
- (let* ((rec (pick (- index last 1) (rest bag)))
- (n (car rec))
- (new-bag (cdr rec)))
- (cons n (cons range new-bag))))))
- (defun generate (count limit)
- (let ((bag `((1 . ,limit)))
- (result nil)
- n pick-result)
- (dotimes (i count)
- (setq pick-result (pick (random (- limit i)) bag))
- (setq n (car pick-result))
- (setq bag (cdr pick-result))
- (setq result (cons n result)))
- result))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement