Debian Codegraph @

ext/mt-random/mt-random.scm

raw
1 ;;;
2 ;;; mt-random - Mersenne Twister interface
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 (define-module math.mt-random
35 (export <mersenne-twister>
36 make-mersenne-twister
37 mt-random-get-seed
38 mt-random-set-seed!
39 mt-random-get-state
40 mt-random-set-state!
41 mt-random-real
42 mt-random-real0
43 mt-random-integer
44 mt-random-fill-u32vector!
45 mt-random-fill-f32vector!
46 mt-random-fill-f64vector!)
47 )
48 (select-module math.mt-random)
49
50 (inline-stub
51 (declcode
52 (.include "mt-random.h"))
53
54 (define-cclass <mersenne-twister> "ScmMersenneTwister*"
55 "Scm_MersenneTwisterClass"
56 (c "SCM_CLASS_DEFAULT_CPL")
57 () ;no external slot
58 (allocator (let* ([seed (Scm_GetKeyword ':seed initargs '#f)]
59 [priv (Scm_GetKeyword ':private? initargs '#f)]
60 [flags::u_int (?: (SCM_FALSEP priv)
61 0
62 SCM_MERSENNE_TWISTER_PRIVATE)])
63 (cast void klass)
64 (return (Scm_MakeMT seed flags)))))
65
66 (define-cproc make-mersenne-twister (:optional (seed #f)
67 (private?::<boolean> #f))
68 (let* ([flags::u_int 0])
69 (when private?
70 (set! flags SCM_MERSENNE_TWISTER_PRIVATE))
71 (return (Scm_MakeMT seed flags))))
72
73 (define-cproc mt-random-get-seed (mt::<mersenne-twister>)
74 (return (-> mt seed)))
75
76 (define-cproc mt-random-set-seed! (mt::<mersenne-twister> init) ::<void>
77 Scm_MTSetSeed)
78
79 (define-cproc mt-random-get-state (mt::<mersenne-twister>)
80 Scm_MTGetState)
81
82 (define-cproc mt-random-set-state! (mt::<mersenne-twister> state::<u32vector>)
83 ::<void>
84 Scm_MTSetState)
85
86 (define-cproc mt-random-real (mt::<mersenne-twister>) ::<double>
87 (return (Scm_MTGenrandF64 mt TRUE)))
88
89 (define-cproc mt-random-real0 (mt::<mersenne-twister>) ::<double>
90 (return (Scm_MTGenrandF64 mt FALSE)))
91
92 (define-cproc %mt-random-integer (mt::<mersenne-twister> n)
93 Scm_MTGenrandInt)
94
95 (define-cproc %mt-random-uint32 (mt::<mersenne-twister>) ::<ulong>
96 Scm_MTGenrandU32)
97
98 (define-cproc mt-random-fill-u32vector! (mt::<mersenne-twister> v::<u32vector>)
99 (return (Scm_MTFillUvector mt (SCM_OBJ v))))
100
101 (define-cproc mt-random-fill-f32vector! (mt::<mersenne-twister> v::<f32vector>)
102 (return (Scm_MTFillUvector mt (SCM_OBJ v))))
103
104 (define-cproc mt-random-fill-f64vector! (mt::<mersenne-twister> v::<f64vector>)
105 (return (Scm_MTFillUvector mt (SCM_OBJ v))))
106 )
107
108 (define (%get-nword-random-int mt n)
109 (let loop ([i 0] [r (%mt-random-uint32 mt)])
110 (if (= i n)
111 r
112 (loop (+ i 1)
113 (+ (ash r 32) (%mt-random-uint32 mt))))))
114
115 (define (mt-random-integer mt n)
116 (when (not (positive? n)) (error "invalid range" n))
117 (if (<= n #x100000000)
118 (%mt-random-integer mt n)
119 (let* ([siz (ash (integer-length n) -5)]
120 [q (quotient (ash 1 (* 32 (+ siz 1))) n)]
121 [qn (* q n)])
122 (let loop ([r (%get-nword-random-int mt siz)])
123 (if (< r qn)
124 (quotient r q)
125 (loop (%get-nword-random-int mt siz)))))))