Function: calcFunc-random

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

Signature

(calcFunc-random MAX)

Source Code

;; Defined in /usr/src/emacs/lisp/calc/calc-comb.el.gz
;;; Produce a random integer or real 0 <= N < MAX.
(defun calcFunc-random (max)
  (cond ((Math-zerop max)
	 (math-gaussian-float))
	((Math-integerp max)
	 (let* ((digs (math-numdigs max))
		(r (math-random-digits (+ digs 3))))
	   (math-mod r max)))
	((Math-realp max)
	 (math-mul (math-random-float) max))
	((and (eq (car max) 'intv) (math-constp max)
	      (Math-lessp (nth 2 max) (nth 3 max)))
	 (if (math-floatp max)
	     (let ((val (math-add (math-mul (math-random-float)
					    (math-sub (nth 3 max) (nth 2 max)))
				  (nth 2 max))))
	       (if (or (and (memq (nth 1 max) '(0 1))      ; almost not worth
			    (Math-equal val (nth 2 max)))  ;   checking!
		       (and (memq (nth 1 max) '(0 2))
			    (Math-equal val (nth 3 max))))
		   (calcFunc-random max)
		 val))
	   (let ((lo (if (memq (nth 1 max) '(0 1))
			 (math-add (nth 2 max) 1) (nth 2 max)))
		 (hi (if (memq (nth 1 max) '(1 3))
			 (math-add (nth 3 max) 1) (nth 3 max))))
	     (if (Math-lessp lo hi)
		 (math-add (calcFunc-random (math-sub hi lo)) lo)
	       (math-reject-arg max "*Empty interval")))))
	((eq (car max) 'vec)
	 (if (cdr max)
	     (nth (1+ (calcFunc-random (1- (length max)))) max)
	   (math-reject-arg max "*Empty list")))
	((and (eq (car max) 'sdev) (math-constp max) (Math-realp (nth 1 max)))
	 (math-add (math-mul (math-gaussian-float) (nth 2 max)) (nth 1 max)))
	(t (math-reject-arg max 'realp))))