random.lsp 2.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sat Sep  6 15:47:42 2003
;;;; Contains: Tests of RANDOM

(in-package :cl-test)

(compile-and-load "numbers-aux.lsp")
(compile-and-load "random-aux.lsp")

(deftest random.error.1
12 13
  (signals-error (random) program-error)
  t)
14 15

(deftest random.error.2
16 17
  (signals-error (random 10 *random-state* nil) program-error)
  t)
18 19

(deftest random.error.3
pfdietz's avatar
pfdietz committed
20
  (check-type-error #'random (typef '(real (0))))
21 22 23 24
  nil)

(deftest random.1
  (loop for i from 2 to 30
sds's avatar
sds committed
25 26 27 28 29 30 31 32 33 34 35
        for n = (ash 1 i)
        nconc
        (loop for j = (1+ (random n))
              repeat 20
              nconc
              (loop for r = (random j)
                    repeat i
                    unless (and (integerp r)
                                (<= 0 r)
                                (< r j))
                    collect (list j r))))
36 37 38 39 40
  nil)


(deftest random.2
  (loop for i from 2 to 20
sds's avatar
sds committed
41 42 43 44 45 46 47 48 49 50 51 52
        for n = (ash 1 i)
        nconc
        (loop for j = (random (float n))
              repeat 20
              unless (zerop j)
              nconc
              (loop for r = (random j)
                    repeat 20
                    unless (and (eql (float r j) r)
                                (<= 0 r)
                                (< r j))
                    collect (list j r))))
53 54 55 56
  nil)

(deftest random.3
  (binomial-distribution-test 10000
sds's avatar
sds committed
57
                              #'(lambda () (eql (random 2) 0)))
58 59 60 61
  t)

(deftest random.4
  (binomial-distribution-test 10000
sds's avatar
sds committed
62
                              #'(lambda () (< (random 1.0s0) 0.5s0)))
63 64 65 66
  t)

(deftest random.5
  (binomial-distribution-test 10000
sds's avatar
sds committed
67
                              #'(lambda () (< (random 1.0d0) 0.5d0)))
68 69 70 71
  t)

(deftest random.6
  (binomial-distribution-test 10000
sds's avatar
sds committed
72
                              #'(lambda () (evenp (random 1024))))
73 74 75 76
  t)

(deftest random.7
  (loop for x in '(10.0s0 20.0f0 30.0d0 40.0l0)
sds's avatar
sds committed
77 78 79
        for r = (random x)
        unless (eql (float r x) r)
        collect (list x r))
80 81
  nil)

82 83
(deftest random.8
  (let* ((f1 '(lambda (x) (random (if x 10 20))))
sds's avatar
sds committed
84
         (f2 (compile nil f1)))
85 86 87 88
    (values
     (loop repeat 100 always (<= 0 (funcall f2 t) 9))
     (loop repeat 100 always (<= 0 (funcall f2 nil) 19))))
  t t)
sds's avatar
sds committed
89

90

91 92
;;; Do more statistical tests here

sds's avatar
sds committed
93