added bytes

master
Avril 5 years ago
parent d7655ae276
commit ac69a923a2
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -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*))

@ -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))

Loading…
Cancel
Save