lvaruzza / cl-randist

Random number generation for common lisp

This URL has Read+Write access

cl-randist / randist.lisp
100644 39 lines (32 sloc) 1.021 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
(in-package :randist)
 
;; On CMUCL or SBCL the random already uses mersene twister
#+(or sbcl cmucl)
(progn
  (declaim (inline random-mt))
  (defun random-mt (x &optional (state *random-state*))
    (random x state)))
 
(defmacro random-uniform ()
  "[syntax suggar] Random variable with uniform distribution in interval [0,1]"
  `(random-mt 1d0))
 
(declaim (ftype (function () double-float) random-pos))
(declaim (inline random-pos))
(defun random-pos ()
  "Create the sign, i.e. random positive or negative, similar to a
binary result."
  (let ((y 0d0))
    (tagbody
     start
       (setf y (random-uniform))
       (when (= y 0d0)
(go start)))
    y))
 
(defun random-vector-iid (n variable)
  "Return a vector with n IID instances of variable"
  (let ((output (make-array n :element-type 'double-float :adjustable nil :fill-pointer nil)))
    (loop for i from 0 to (1- n)
       do (setf (aref output i) (funcall variable)))
    output))
 
(defun random-bernoulli (p)
  (if (> (random-uniform) p)
      1
      0))