From 318c88aaf1033d452a5449eba45aa382988412f7 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sun, 16 Oct 2022 23:35:21 -0700 Subject: [PATCH 03/10] =?UTF-8?q?Simplify=20calc-comb=20by=20using=20?= =?UTF-8?q?=E2=80=98random=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * lisp/calc/calc-comb.el (math-random-table, math-last-RandSeed) (math-random-ptr1, math-random-ptr2, math-random-shift) (var-RandSeed, math-random-cache, math-init-random-base) (math-random-base, math-random-last) (math-random-three-digit-number): Now obsolete, as we can assume that ‘random’ is good enough. (math-random-digits): Simplify by using ‘random’. --- lisp/calc/calc-comb.el | 96 ++++++++--------------------------------- lisp/calc/calc-ext.el | 6 ++- lisp/calc/calc-stuff.el | 4 +- 3 files changed, 23 insertions(+), 83 deletions(-) diff --git a/lisp/calc/calc-comb.el b/lisp/calc/calc-comb.el index c1352fa324..522a7ac6c0 100644 --- a/lisp/calc/calc-comb.el +++ b/lisp/calc/calc-comb.el @@ -546,105 +546,42 @@ math-stirling-2 (math-mul m (math-stirling-2 (1- n) m)))))) (defvar math-random-table nil) +(make-obsolete-variable 'math-random-table nil "29.1") (defvar math-last-RandSeed nil) +(make-obsolete-variable 'math-last-RandSeed nil "29.1") (defvar math-random-ptr1 nil) +(make-obsolete-variable 'math-random-ptr1 nil "29.1") (defvar math-random-ptr2 nil) -(defvar math-random-shift nil) +(make-obsolete-variable 'math-random-ptr2 nil "29.1") +(defvar math-random-shift -4) ; assume RAND_MAX >= 16383 +(make-obsolete-variable 'math-random-shift nil "29.1") -;;; Produce a random 10-bit integer, with (random) if no seed provided, -;;; or else with Numerical Recipes algorithm ran3 / Knuth 3.2.2-A. +;;; Produce a random 10-bit integer. (defvar var-RandSeed) +(make-obsolete-variable 'var-RandSeed nil "29.1") (defvar math-random-cache nil) -(defvar math-gaussian-cache nil) +(make-obsolete-variable 'math-random-cache nil "29.1") (defun math-init-random-base () - (if (and (boundp 'var-RandSeed) var-RandSeed) - (if (eq (car-safe var-RandSeed) 'vec) - nil - (if (Math-integerp var-RandSeed) - (let* ((seed (math-sub 161803 var-RandSeed)) - (mj (1+ (math-mod seed 1000000))) - (mk (1+ (math-mod (math-quotient seed 1000000) - 1000000))) - (i 0)) - (setq math-random-table (cons 'vec (make-list 55 mj))) - (while (<= (setq i (1+ i)) 54) - (let* ((ii (% (* i 21) 55)) - (p (nthcdr ii math-random-table))) - (setcar p mk) - (setq mk (- mj mk) - mj (car p))))) - (math-reject-arg var-RandSeed "*RandSeed must be an integer")) - (setq var-RandSeed (list 'vec var-RandSeed) - math-random-ptr1 math-random-table - math-random-cache nil - math-random-ptr2 (nthcdr 31 math-random-table)) - (let ((i 200)) - (while (> (setq i (1- i)) 0) - (math-random-base)))) - (setq var-RandSeed nil - math-random-cache nil - math-random-shift -4) ; assume RAND_MAX >= 16383 - ;; This exercises the random number generator and also helps - ;; deduce a better value for RAND_MAX. - (let ((i 0)) - (while (< (setq i (1+ i)) 30) - (if (> (ash (math-abs (random)) math-random-shift) 4095) - (setq math-random-shift (1- math-random-shift)))))) - (setq math-last-RandSeed var-RandSeed - math-gaussian-cache nil)) + (declare (obsolete nil "29.1"))) (defun math-random-base () - (if var-RandSeed - (progn - (setq math-random-ptr1 (or (cdr math-random-ptr1) - (cdr math-random-table)) - math-random-ptr2 (or (cdr math-random-ptr2) - (cdr math-random-table))) - (logand (ash (setcar math-random-ptr1 - (logand (- (car math-random-ptr1) - (car math-random-ptr2)) 524287)) - -6) 1023)) - (logand (ash (random) math-random-shift) 1023))) - + (declare (obsolete 'random "29.1")) + (random 1024)) ;;; Produce a random digit in the range 0..999. -;;; Avoid various pitfalls that may lurk in the built-in (random) function! -;;; Shuffling algorithm from Numerical Recipes, section 7.1. (defvar math-random-last) +(make-obsolete-variable 'math-random-last nil "29.1") (defun math-random-three-digit-number () "Return a random three digit number." - (let (i) - (or (and (boundp 'var-RandSeed) (eq var-RandSeed math-last-RandSeed)) - (math-init-random-base)) - (or math-random-cache - (progn - (setq math-random-last (math-random-base) - math-random-cache (make-vector 13 nil) - i -1) - (while (< (setq i (1+ i)) 13) - (aset math-random-cache i (math-random-base))))) - (while (progn - (setq i (/ math-random-last 79) ; 0 <= i < 13 - math-random-last (aref math-random-cache i)) - (aset math-random-cache i (math-random-base)) - (>= math-random-last 1000))) - math-random-last)) + (declare (obsolete 'random "29.1")) + (random 1000)) ;;; Produce an N-digit random integer. (defun math-random-digits (n) "Produce a random N digit integer." - (let* ((slop (% (- 3 (% n 3)) 3)) - (i (/ (+ n slop) 3)) - (rnum 0)) - (while (> i 0) - (setq rnum - (math-add - (math-random-three-digit-number) - (math-mul rnum 1000))) - (setq i (1- i))) - (math-normalize (math-scale-right rnum slop)))) + (random (expt 10 n))) ;;; Produce a uniformly-distributed random float 0 <= N < 1. (defun math-random-float () @@ -652,6 +589,7 @@ math-random-float (- calc-internal-prec))) ;;; Produce a Gaussian-distributed random float with mean=0, sigma=1. +(defvar math-gaussian-cache nil) (defun math-gaussian-float () (math-with-extra-prec 2 (if (and math-gaussian-cache diff --git a/lisp/calc/calc-ext.el b/lisp/calc/calc-ext.el index 7ee73d100a..55808d28b1 100644 --- a/lisp/calc/calc-ext.el +++ b/lisp/calc/calc-ext.el @@ -785,8 +785,10 @@ calc-init-extensions calcFunc-gcd calcFunc-lcm calcFunc-moebius calcFunc-nextprime calcFunc-perm calcFunc-prevprime calcFunc-prfac calcFunc-prime calcFunc-random calcFunc-shuffle calcFunc-stir1 calcFunc-stir2 -calcFunc-totient math-init-random-base math-member math-prime-test -math-random-base) +calcFunc-totient +math-init-random-base ; obsolescent as of 29.1 +math-random-base ; obsolescent as of 29.1 +math-member math-prime-test) ("calccomp" calcFunc-cascent calcFunc-cdescent calcFunc-cheight calcFunc-cwidth math-comp-ascent math-comp-descent diff --git a/lisp/calc/calc-stuff.el b/lisp/calc/calc-stuff.el index 758b920184..87a7eb6e65 100644 --- a/lisp/calc/calc-stuff.el +++ b/lisp/calc/calc-stuff.el @@ -155,7 +155,7 @@ math-lud-cache (defvar math-log2-cache) ; calc-bin.el (defvar math-radix-digits-cache) ; calc-bin.el (defvar math-radix-float-cache-tag) ; calc-bin.el -(defvar math-random-cache) ; calc-comb.el +(defvar math-random-cache) ; calc-comb.el; obsolescent as of 29.1 (defvar math-max-digits-cache) ; calc-bin.el (defvar math-integral-cache) ; calcalg2.el (defvar math-units-table) ; calc-units.el @@ -170,7 +170,7 @@ calc-flush-caches math-log2-cache nil math-radix-digits-cache nil math-radix-float-cache-tag nil - math-random-cache nil + math-random-cache nil ; obsolescent as of 29.1 math-max-digits-cache nil math-integral-cache nil math-units-table nil -- 2.34.1