source: project/release/4/srfi-27/trunk/srfi-27.scm @ 34210

Last change on this file since 34210 was 34210, checked in by Kon Lovett, 2 years ago

re-flow, use macros, preds for types, more tests

File size: 8.2 KB
Line 
1;;;; srfi-27.scm
2;;;; Kon Lovett, Jun '17
3;;;; Kon Lovett, Oct '09
4
5(module srfi-27
6
7(;export
8  ;; SRFI 27
9  default-random-source
10  random-integer
11  random-real
12  make-random-source
13  random-source? check-random-source error-random-source
14  random-source-state-ref
15  random-source-state-set!
16  random-source-randomize!
17  random-source-pseudo-randomize!
18  random-source-make-integers
19  random-source-make-reals
20  ;; Extensions
21  ;
22  registered-random-sources registered-random-source
23  current-random-source
24  random-integer/current random-real/current
25  new-random-source
26  random-source-name random-source-kind
27  random-source-documentation
28  random-source-log2-period
29  random-source-maximum-range
30  random-source-entropy-source random-source-entropy-source-set!
31  random-source-make-u8vectors
32  random-source-make-f64vectors
33  random-u8vector
34  random-f64vector
35  ;
36  registered-entropy-sources registered-entropy-source
37  current-entropy-source
38  make-entropy-source new-entropy-source
39  entropy-source? check-entropy-source error-entropy-source
40  entropy-source-name entropy-source-kind
41  entropy-source-documentation
42  entropy-source-u8
43  entropy-source-f64
44  entropy-source-u8vector
45  entropy-source-f64vector)
46
47(import scheme)
48
49(import chicken)
50
51(import
52  (only data-structures alist-ref alist-update!)
53  (only srfi-4 make-u8vector make-f64vector))
54(require-library data-structures srfi-4)
55
56(import (only miscmacros define-parameter))
57(require-library miscmacros)
58
59(import
60  (only type-errors error-argument-type warning-argument-type)
61  (only srfi-27-numbers check-real-precision)
62  (only srfi-27-vector-support u8vector-filled! f64vector-filled!))
63(require-library
64  type-errors
65  srfi-27-numbers srfi-27-vector-support)
66
67(use
68  type-checks srfi-4-checks
69  random-source mrg32k3a
70  entropy-source entropy-clock)
71
72;;; Entropy Source
73
74;;
75
76(define default-entropy-source (make-entropy-source-system-clock))
77
78;;
79
80(define-parameter current-entropy-source default-entropy-source
81  (lambda (x)
82    (cond
83      ((entropy-source? x)
84        x )
85      (else
86        (warning-argument-type 'current-entropy-source x 'entropy-source)
87        (current-entropy-source) ) ) ) )
88
89(define (make-entropy-source #!optional (es (current-entropy-source)))
90  (let ((ctor
91          (cond
92            ((entropy-source? es)
93              (@entropy-source-constructor es) )
94            ((symbol? es)
95              (registered-entropy-source es) )
96            (else
97              #f ) ) ) )
98    (unless ctor
99      (error-argument-type
100        'make-entropy-source es
101        "valid entropy-source or registered entropy-source name") )
102    (ctor) ) )
103
104(define (new-entropy-source es)
105  ((@entropy-source-constructor (check-entropy-source 'new-entropy-source es))) )
106
107(define (entropy-source-name es)
108  (*entropy-source-name (check-entropy-source 'entropy-source-name es)) )
109
110(define entropy-source-kind entropy-source-name)
111
112(define (entropy-source-documentation es)
113  (*entropy-source-documentation
114    (check-entropy-source 'entropy-source-documentation es)) )
115
116(define (entropy-source-u8vector es n #!optional vec)
117  ((@entropy-source-u8vector (check-entropy-source 'entropy-source-u8vector es))
118    (check-positive-fixnum 'entropy-source-u8vector n)
119    (and vec (check-u8vector 'entropy-source-u8vector vec))) )
120
121(define (entropy-source-f64vector es n #!optional vec)
122  ((@entropy-source-f64vector (check-entropy-source 'entropy-source-f64vector es))
123    (check-positive-fixnum 'entropy-source-f64vector n)
124    (and vec (check-f64vector 'entropy-source-f64vector vec))) )
125
126(define (entropy-source-u8 es)
127  (@entropy-source-u8 (check-entropy-source 'entropy-source-u8 es)) )
128
129(define (entropy-source-f64 es)
130  (@entropy-source-f64 (check-entropy-source 'entropy-source-f64 es)) )
131
132;;; Random Source
133
134(define (*random-source-make-u8vectors rs)
135  (let ((rndint ((@random-source-make-integers rs))))
136    (lambda (n)
137      (u8vector-filled!
138        (make-u8vector (check-cardinal-integer 'random-source-make-u8vector n 'length))
139        (lambda () (rndint 256))) ) ) )
140
141(define (*random-source-make-f64vectors rs prec)
142  (let ((rnd ((@random-source-make-reals rs) prec)))
143    (lambda (n)
144      (f64vector-filled!
145        (make-f64vector (check-cardinal-integer 'random-source-make-f64vector n 'length))
146        rnd) ) ) )
147
148;;
149
150(define default-random-source (make-random-source-mrg32k3a))
151
152(define random-integer ((@random-source-make-integers default-random-source)))
153
154(define random-real ((@random-source-make-reals default-random-source) #f))
155
156(define random-u8vector
157  (let ((mkv (*random-source-make-u8vectors default-random-source)))
158    (lambda (n) (mkv n) ) ) )
159
160(define random-f64vector
161  (let ((mkv (*random-source-make-f64vectors default-random-source #f)))
162    (lambda (n) (mkv n) ) ) )
163
164;;
165
166(define-parameter current-random-source default-random-source
167  (lambda (x)
168    (cond
169      ((random-source? x)
170        x )
171      (else
172        (warning-argument-type 'current-random-source x 'random-source)
173        (current-random-source) ) ) ) )
174
175(define (random-integer/current)
176  ((@random-source-make-integers (current-random-source))) )
177
178(define (random-real/current)
179  ((@random-source-make-reals (current-random-source)) #f) )
180
181(define (make-random-source #!optional (rs (current-random-source)))
182  (let ((ctor
183          (cond
184            ((random-source? rs)
185              (@random-source-constructor rs) )
186            ((symbol? rs)
187              (registered-random-source rs) )
188            (else
189              #f ) ) ) )
190    (unless ctor
191      (error-argument-type
192        'make-random-source rs
193        "valid random-source or registered random-source name") )
194    (ctor) ) )
195
196(define (new-random-source #!optional (rs (current-random-source)))
197  ((@random-source-constructor (check-random-source 'new-random-source rs))) )
198
199(define (random-source-name rs)
200  (*random-source-name
201    (check-random-source 'random-source-name rs)) )
202
203(define random-source-kind random-source-name)
204
205(define (random-source-documentation rs)
206  (*random-source-documentation
207    (check-random-source 'random-source-documentation rs)) )
208
209(define (random-source-log2-period rs)
210  (*random-source-log2-period
211    (check-random-source 'random-source-log2-period rs)) )
212
213(define (random-source-maximum-range rs)
214  (*random-source-maximum-range
215    (check-random-source 'random-source-maximum-range rs)) )
216
217(define (random-source-entropy-source rs)
218  (*random-source-entropy-source
219    (check-random-source 'random-source-entropy-source rs)) )
220
221(define (random-source-entropy-source-set! rs es)
222  (*random-source-entropy-source-set!
223    (check-random-source 'random-source-entropy-source-set! rs)
224    ;#f indicates no set entropy-source
225    (and es (check-entropy-source 'random-source-entropy-source-set! es))) )
226
227(define (random-source-state-ref rs)
228  ((@random-source-state-ref (check-random-source 'random-source-state-ref rs))) )
229
230(define (random-source-state-set! rs state)
231  ((@random-source-state-set! (check-random-source 'random-source-state-set! rs))
232    state) )
233
234(define (random-source-randomize! rs #!optional es)
235  ((@random-source-randomize! (check-random-source 'random-source-randomize! rs))
236    (or
237      (and es (check-entropy-source 'random-source-randomize! es))
238      (*random-source-entropy-source rs)
239      (current-entropy-source))) )
240
241(define (random-source-pseudo-randomize! rs i j)
242  ((@random-source-pseudo-randomize! (check-random-source 'random-source-pseudo-randomize! rs))
243    (check-cardinal-integer 'random-source-pseudo-randomize! i)
244    (check-cardinal-integer 'random-source-pseudo-randomize! j)) )
245
246(define (random-source-make-integers rs)
247  ((@random-source-make-integers (check-random-source 'random-source-make-integers rs))) )
248
249(define (random-source-make-reals rs #!optional prec)
250  ((@random-source-make-reals (check-random-source 'random-source-make-reals rs))
251    (and prec (check-real-precision 'random-source-make-reals prec 'precision))) )
252
253(define (random-source-make-u8vectors rs)
254  (*random-source-make-u8vectors
255    (check-random-source 'random-source-make-u8vectors rs)) )
256
257(define (random-source-make-f64vectors rs #!optional prec)
258  (*random-source-make-f64vectors
259    (check-random-source 'random-source-make-f64vectors rs)
260    (and prec (check-real-precision 'random-source-make-f64vectors prec 'precision))) )
261
262) ;module srfi-27
Note: See TracBrowser for help on using the repository browser.