Skip to content

Commit 6d98fae

Browse files
committed
added random-number-generator-tests
added tests fixed method next for gsll-rng to return the correct type
1 parent b2f3c2a commit 6d98fae

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

cl-random.asd

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
:serial t
4141
:components
4242
((:file "setup")
43+
(:file "random-number-generator")
4344
(:file "discrete")
4445
(:file "univariate")
4546
(:file "continuous-time")

src/random-number-generator.lisp

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@
6969

7070
(defmethod next ((rng gsll-rng) (limit fixnum) &optional pos)
7171
(do ((r (gsll:sample (state rng) :uniform-fixnum :upperbound limit)))
72-
((not (and pos (zerop r))) (float r 0d0))))
72+
((not (and pos (zerop r))) r)))
7373

7474
(defmethod next ((rng gsll-rng) (limit float) &optional pos)
7575
(do ((r (gsll:sample (state rng) :flat :a 0.0 :b limit)))
76-
((not (and pos (zerop r))) (float r 0d0))))
76+
((not (and pos (zerop r))) (float r limit))))
7777

7878

7979
;; TODO: Add MT19337 from cl-randist.

tests/random-number-generator.lisp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
;;; -*- Mode:Lisp; Syntax:ANSI-Common-Lisp; Coding:utf-8 -*-
2+
3+
(in-package #:cl-random-tests)
4+
5+
(defun valid-rng (rng &key (n 100000) (limit 100) (z-band 4d0) (var-band 0.1))
6+
"Get N random numbers in [0,LIMIT) from RNG. Verify their type, range, and moments, using
7+
8+
1. the z-score of the mean (should be below Z-BAND in absolute value),
9+
10+
2. the variance with NUM= using VAR-BAND as a tolerance."
11+
(let ((mean (/ (if (integerp limit) (1- limit) limit) 2))
12+
(variance (/ (if (integerp limit) (1- (* limit limit)) (* limit limit)) 12))
13+
(sample-moments (clnu.stats:central-sample-moments nil)))
14+
(loop repeat n for r = (next rng limit)
15+
unless (and (<= 0 r) (< r limit)) return NIL
16+
unless (typep r (type-of limit)) return NIL
17+
do (clnu.stats:add sample-moments r))
18+
(and (< (z-score n mean variance (clnu.stats:mean sample-moments)) z-band)
19+
(num= variance (clnu.stats:variance sample-moments) var-band))))
20+
21+
(defsuite random-number-generator-tests (tests))
22+
23+
(deftest cl-rng-tests (random-number-generator-tests)
24+
(assert-true (valid-rng (make-instance 'cl-rng) :limit 100))
25+
(assert-true (valid-rng (make-instance 'cl-rng) :limit 100.0))
26+
(assert-true (valid-rng (make-instance 'gsll-rng) :limit 100))
27+
(assert-true (valid-rng (make-instance 'gsll-rng) :limit 100.0)))

0 commit comments

Comments
 (0)