| 1 |
;;; |
| 2 |
;;; SRFI-27.SCM - Sources of Random Bits |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2000-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
;; Implements SRFI-27 interface on top of math.mt-random module. |
| 35 |
|
| 36 |
(define-module srfi.27 |
| 37 |
(use math.mt-random) |
| 38 |
(use gauche.uvector) |
| 39 |
(use binary.io) |
| 40 |
(export random-integer random-real default-random-source |
| 41 |
make-random-source random-source? |
| 42 |
random-source-state-ref random-source-state-set! |
| 43 |
random-source-randomize! random-source-pseudo-randomize! |
| 44 |
random-source-make-integers random-source-make-reals |
| 45 |
)) |
| 46 |
(select-module srfi.27) |
| 47 |
|
| 48 |
;; Operations on random source |
| 49 |
(define (make-random-source) (make-mersenne-twister)) |
| 50 |
(define (random-source? obj) (is-a? obj <mersenne-twister>)) |
| 51 |
(define default-random-source (make-random-source)) |
| 52 |
|
| 53 |
(define (random-source-state-ref source) |
| 54 |
(mt-random-get-state source)) |
| 55 |
(define (random-source-state-set! source state) |
| 56 |
(mt-random-set-state! source state)) |
| 57 |
|
| 58 |
;; Randomize |
| 59 |
(define (random-source-randomize! source) |
| 60 |
(assume (random-source? source)) |
| 61 |
(cond |
| 62 |
[(sys-access "/dev/urandom" R_OK) |
| 63 |
(call-with-input-file "/dev/urandom" |
| 64 |
(^p |
| 65 |
(let1 seedv (make-u32vector 4) |
| 66 |
(u32vector-set! seedv 0 (read-u32 p)) |
| 67 |
(u32vector-set! seedv 1 (read-u32 p)) |
| 68 |
(u32vector-set! seedv 2 (read-u32 p)) |
| 69 |
(u32vector-set! seedv 3 (read-u32 p)) |
| 70 |
(mt-random-set-seed! source seedv))))] |
| 71 |
[else |
| 72 |
(let1 t (current-time) |
| 73 |
(mt-random-set-seed! source |
| 74 |
(* (~ t'second) (~ t'nanosecond) |
| 75 |
(sys-getpid))))])) |
| 76 |
|
| 77 |
(define (random-source-pseudo-randomize! source i j) |
| 78 |
;; This procedure is effectively required to map integers (i,j) into |
| 79 |
;; a seed value in a deterministic way. Talking advantage of the fact |
| 80 |
;; that Mersenne Twister can take vector of numbers. |
| 81 |
|
| 82 |
;; interleave-i and interleave-j creates a list of integers, each |
| 83 |
;; is less than 2^32, consisted by interleaving each 32-bit chunk of i and j. |
| 84 |
(define (interleave-i i j lis) |
| 85 |
(if (zero? i) |
| 86 |
(if (zero? j) lis (interleave-j 0 j (cons 0 lis))) |
| 87 |
(receive (q r) (quotient&remainder i #x100000000) |
| 88 |
(interleave-j q j (cons r lis))))) |
| 89 |
|
| 90 |
(define (interleave-j i j lis) |
| 91 |
(if (zero? j) |
| 92 |
(if (zero? i) lis (interleave-i i 0 (cons 0 lis))) |
| 93 |
(receive (q r) (quotient&remainder j #x100000000) |
| 94 |
(interleave-i i q (cons r lis))))) |
| 95 |
|
| 96 |
;; main body |
| 97 |
(assume (random-source? source)) |
| 98 |
(when (or (not (integer? i)) (not (integer? j)) |
| 99 |
(negative? i) (negative? j)) |
| 100 |
(errorf "indices must be non-negative integers: ~s, ~s" i j)) |
| 101 |
(mt-random-set-seed! source |
| 102 |
(list->u32vector (interleave-i i j '(#xffffffff)))) |
| 103 |
) |
| 104 |
|
| 105 |
;; Obtain generators from random source. |
| 106 |
(define-inline (random-source-make-integers source) |
| 107 |
(assume (random-source? source)) |
| 108 |
(^n (mt-random-integer source n))) |
| 109 |
|
| 110 |
(define-inline random-source-make-reals |
| 111 |
(case-lambda |
| 112 |
[(source) |
| 113 |
(assume (random-source? source)) |
| 114 |
(^[] (mt-random-real source))] |
| 115 |
[(source unit) |
| 116 |
(assume (random-source? source)) |
| 117 |
(unless (< 0 unit 1) |
| 118 |
(error "unit must be between 0.0 and 1.0 (exclusive), but got" unit)) |
| 119 |
(let* ([1/unit (/ unit)] |
| 120 |
[range (- (floor->exact 1/unit) 1)]) |
| 121 |
(^[] (/ (+ 1 (mt-random-integer source range)) 1/unit)))])) |
| 122 |
|
| 123 |
;; Default random generators. |
| 124 |
(define-values (random-integer random-real) |
| 125 |
(let1 src default-random-source |
| 126 |
(values (^n (mt-random-integer src n)) |
| 127 |
(^[] (mt-random-real src))))) |