Implementation of make-random-real-generator
Bradley Lucier 05 May 2020 01:07 UTC
make-random-real-generator is defined as
(define make-random-real-generator
(case-lambda
((low-bound up-bound)
(make-random-real-generator default-random-source low-bound up-bound))
((rand-src low-bound up-bound)
(let* ((rand-int-proc (random-source-make-integers rand-src))
(steps (expt 2 32))
(rand-real-proc (lambda ()
(/ (inexact (rand-int-proc steps))
(- steps 1))))
(range (- up-bound low-bound)))
(lambda ()
(+ low-bound (* (rand-real-proc) range)))))))
I'm curious, if you're basing this SRFI on SRFI 27, why you decided to
build your own rand-real-proc based on randomly generated integers
rather than simply use random-real from SRFI 27.
For implementations with unboxed 64-bit IEEE floating-point arithmetic,
the fundamental generator for SRFI 27 is most likely written using
floating-point, and returns a float strictly between 0 and 1.
Since in many implementations SRFI 27's random-real would be faster and
would serve (almost) as good a purpose as the current implementation, I
recommend using it here.
Brad
Some secondary issues:
It's true that random-real can take slightly fewer than (expt 2 32)
possible values, while your generator can tabut that doesn't seem to be
an issue here.
A secondary issue is that the expression for rand-real-proc mixes
inexact and exact arithmetic, which can lead to run-time slowdowns.