540 |
;;; Produce a random 10-bit integer, with (random) if no seed provided, |
;;; Produce a random 10-bit integer, with (random) if no seed provided, |
541 |
;;; or else with Numerical Recipes algorithm ran3 / Knuth 3.2.2-A. |
;;; or else with Numerical Recipes algorithm ran3 / Knuth 3.2.2-A. |
542 |
|
|
543 |
(defvar var-RandSeed nil) |
(defvar var-RandSeed) |
544 |
(defvar math-random-cache nil) |
(defvar math-random-cache nil) |
545 |
(defvar math-gaussian-cache nil) |
(defvar math-gaussian-cache nil) |
546 |
|
|
547 |
(defun math-init-random-base () |
(defun math-init-random-base () |
548 |
(if var-RandSeed |
(if (and (boundp 'var-RandSeed) var-RandSeed) |
549 |
(if (eq (car-safe var-RandSeed) 'vec) |
(if (eq (car-safe var-RandSeed) 'vec) |
550 |
nil |
nil |
551 |
(if (Math-integerp var-RandSeed) |
(if (Math-integerp var-RandSeed) |
599 |
;;; Produce a random digit in the range 0..999. |
;;; Produce a random digit in the range 0..999. |
600 |
;;; Avoid various pitfalls that may lurk in the built-in (random) function! |
;;; Avoid various pitfalls that may lurk in the built-in (random) function! |
601 |
;;; Shuffling algorithm from Numerical Recipes, section 7.1. |
;;; Shuffling algorithm from Numerical Recipes, section 7.1. |
602 |
|
(defvar math-random-last) |
603 |
(defun math-random-digit () |
(defun math-random-digit () |
604 |
(let (i math-random-last) |
(let (i) |
605 |
(or (and var-RandSeed (eq var-RandSeed math-last-RandSeed)) |
(or (and (boundp 'var-RandSeed) (eq var-RandSeed math-last-RandSeed)) |
606 |
(math-init-random-base)) |
(math-init-random-base)) |
607 |
(or math-random-cache |
(or math-random-cache |
608 |
(progn |
(progn |