diff --git a/cl-rng.lisp b/cl-rng.lisp index f0708fe..c0a31e0 100644 --- a/cl-rng.lisp +++ b/cl-rng.lisp @@ -2,7 +2,9 @@ (in-package :cl-rng) -(defparameter *default-randomness-provider* #'urandom +(defparameter *default-precision* 100 + "Default precision for the default RNG provider") +(defparameter *default-randomness-provider* #'(lambda (&rest args) (apply #'urandom (append `(:precision ,*default-precision*) args))) "The default randomness provider used by cl-rng functions") (defun chance (fraction &key (provider *default-randomness-provider*)) diff --git a/urandom.lisp b/urandom.lisp index 1c23e0f..2c53abe 100644 --- a/urandom.lisp +++ b/urandom.lisp @@ -11,7 +11,7 @@ (let* ((val (apply '+ (loop for i from 0 below precision collect (read-byte rng)))) (max (* precision 255)) (frac (/ val max))) - (values (* limit frac) frac)))))) + (values (* limit frac) val)))))) (defun %urandom-vector (range &rest params) (loop for x from 0 below (length range) collect @@ -27,8 +27,17 @@ (apply '%urandom-list (cons range params)) (apply '%urandom-vector (cons range params)))) +(defun urandom-bytes (len &key (transform #'identity) (type :vector)) + (with-open-file (rng "/dev/urandom" :element-type 'unsigned-byte) + (let ((vec (make-array len))) + (loop for i from 0 below len do (setf (aref vec i) (funcall transform (read-byte rng)))) + (cond ((eq type :vector) vec) + ((eq type :list) (coerce vec 'list)) + (t (coerce vec type)))))) + (export 'urandom) (export 'urandom-range) +(export 'urandom-bytes) ;(defparameter *dice-results* (make-list 10))