Function: calcFunc-shuffle

calcFunc-shuffle is an autoloaded and byte-compiled function defined in calc-comb.el.gz.

Signature

(calcFunc-shuffle N &optional MAX)

Source Code

;; Defined in /usr/src/emacs/lisp/calc/calc-comb.el.gz
;;; Choose N objects at random from the set MAX without duplicates.
(defun calcFunc-shuffle (n &optional max)
  (or max (setq max n n -1))
  (or (and (Math-num-integerp n)
	   (or (natnump (setq n (math-trunc n))) (eq n -1)))
      (math-reject-arg n 'integerp))
  (cond ((or (math-zerop max)
	     (math-floatp max)
	     (eq (car-safe max) 'sdev))
	 (if (< n 0)
	     (math-reject-arg n 'natnump)
	   (math-simple-shuffle n max)))
	((and (<= n 1) (>= n 0))
	 (math-simple-shuffle n max))
	((and (eq (car-safe max) 'intv) (math-constp max))
	 (let ((num (math-add (math-sub (nth 3 max) (nth 2 max))
			      (cdr (assq (nth 1 max)
					 '((0 . -1) (1 . 0)
					   (2 . 0) (3 . 1))))))
	       (min (math-add (nth 2 max) (if (memq (nth 1 max) '(0 1))
					      1 0))))
	   (if (< n 0) (setq n num))
	   (or (math-posp num) (math-reject-arg max 'range))
	   (and (Math-lessp num n) (math-reject-arg n 'range))
	   (if (Math-lessp n (math-quotient num 3))
	       (math-simple-shuffle n max)
	     (if (> (* n 4) (* num 3))
		 (math-add (math-sub min 1)
			   (math-shuffle-list n num (calcFunc-index num)))
	       (let ((tot 0)
		     (m 0)
		     (vec nil))
		 (while (< m n)
		   (if (< (calcFunc-random (- num tot)) (- n m))
		       (setq vec (cons (math-add min tot) vec)
			     m (1+ m)))
		   (setq tot (1+ tot)))
		 (math-shuffle-list n n (cons 'vec vec)))))))
	((eq (car-safe max) 'vec)
	 (let ((size (1- (length max))))
	   (if (< n 0) (setq n size))
	   (if (and (> n (/ size 2)) (<= n size))
	       (math-shuffle-list n size (copy-sequence max))
	     (let* ((vals (calcFunc-shuffle
			   n (list 'intv 3 1 (1- (length max)))))
		    (p vals))
	       (while (setq p (cdr p))
		 (setcar p (nth (car p) max)))
	       vals))))
	((math-integerp max)
	 (if (math-posp max)
	     (calcFunc-shuffle n (list 'intv 2 0 max))
	   (calcFunc-shuffle n (list 'intv 1 max 0))))
	(t (math-reject-arg max 'realp))))