[erlang-questions] On OTP rand module difference between OTP 19 and OTP 20
Richard A. O'Keefe
<>
Mon Sep 4 02:37:50 CEST 2017
On 1/09/17 8:49 PM, Raimo Niskanen wrote:
>> By the way, given that a common way to make random floats is to
>> generate a bitvector, consider
>> (0 to: 15) collect: [:each | ((each / 15) * 256) truncated].
>> You will notice that the spacing between the values is *almost*
>> uniform, but not at the end.
>
> That sounds interesting but I do not understand. Is that Elixir code?
Nope, Smalltalk. I wanted to use rational arithmetic. In fact I did
not need to. Here it is in Haskell:
> [(x * 256) `div` 15 | x <- [0..15]]
[0,17,34,51,68,85,102,119,136,153,170,187,204,221,238,256]
Let's push that a bit further. Let's generate all possible 10-bit
integers and map them to the range [0..63]. We find again that
the gap sizes are not all the same. They can't be. If you
consider all vectors of N bits and map them to the range
[0..2**M] they *cannot* be uniformly distributed no matter what
method you use because (2**M+1) does not divide 2**N. You can
fix this by rejecting some of the bit vectors, but that would
be asking everyone to pay extra for a result they don't have any
particular need for.
More information about the erlang-questions
mailing list