;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; This file is part of "NOVA": NOVA = search + COCOMO tools ; Copyright, 2008, Tim Menzies tim@menzies.us ; ; NOVA is free software: you can redistribute it and/or modify ; it under the terms of the GNU General Public License as published by ; the Free Software Foundation, either version 3 of the License, or ; (at your option) any later version. ; ; NOVA is distributed in the hope that it will be useful, ; but WITHOUT ANY WARRANTY; without even the implied warranty of ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; GNU General Public License for more details. ; You should have received a copy of the GNU General Public License ; a long with NOVA. If not, see . ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (in-package :wvu-lib.tricks) (defparameter *seed0* 10013) (defparameter *seed* *seed0*) (defun reset-seed (&optional seed) (setf *seed* (or seed *seed0*))) (defun park-miller-randomizer () "The Park-Miller multiplicative congruential randomizer (CACM, October 88, Page 1195). Creates pseudorandom floating-point numbers in the range 0 < x <= 1." (let ((multiplier 16807d0) ; 16807 = (expt 7 5) (modulus 2147483647d0)) ; 2147483647 = (- (expt 2 31) 1) (let ((temp (* multiplier *seed*))) (setf *seed* (mod temp modulus)) (/ *seed* modulus)))) (defun my-random (n) "Returns a pseudorandom floating-point number in the range 0 <= x < n." (let ((x (park-miller-randomizer))) ;; x is in the range 0 < x <= 1 ;; subtract from 1 to end up in the range 0 <= x < 1 (* n (- 1 x)))) (defun my-random-int (n) "Returns a pseudorandom integer in the range 0 <= x <= n - 1." (let ((x (my-random 1))) (floor (* n x))))