Re: Implementation of make-random-real-generator
Bradley Lucier 05 May 2020 19:08 UTC
On 5/5/20 2:48 PM, Arvydas Silanskas wrote:
>> 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
>
> The only reason is because it was defined for the generator to return
> lower and upper bounds inclusively,
Meeting the spec is a great reason!
But as Marc pointed out, sampling from (a,b) (exclusive) is the same as
sampling from [a,b] (inclusive), because the probability of choosing a
or b is 0.
So I recommend using random-real to get a random float between 0 and 1
(exclusive, as it is implemented).
It could still happen, however, that the expression
(+ a (* (- b a) (random-real)))
could return a or b because of rounding, e.g., if
a = 1e15
b = 1e15 + 1.
then 1e15 + (random-real) could return either 1e15 (if (random-real) is
close enough to 0) or 1e15+1. (if (random-real) is close enough to 1).
So documenting the possibility of returning a or b is a good thing, but
the implementation should just use SRFI 27's built in (random-real).
Brad