[erlang-questions] On OTP rand module difference between OTP 19 and OTP 20

Richard A. O'Keefe ok@REDACTED
Mon Sep 4 03:42:19 CEST 2017



On 2/09/17 12:53 AM, Raimo Niskanen wrote:

>>    P = random(),
>>    if
>>       P =< 0.3 -> ...;
>>       P =< 0.7 -> ...;
>>       P > 0.7 -> ...
>>    end

> Especially since decimal numbers below 1.0 have no exact representation as
> IEEE floating point numbers.

There is in fact an IEEE standard for *decimal* floating point numbers.
If memory serves me correctly, z/Series and POWER support it.  There is
a supplement to the C standard for it.  Software emulation is available.
See decimal32, decimal64, decimal128 in
https://en.wikipedia.org/wiki/IEEE_754
or the actual IEEE 754-2008 if you have a copy (which I sadly don't).

In any case,
     P = 10*rand(),
     if P <  3 -> ...
      ; P <  7 -> ...
      ; P >= 7 -> ...
     end
evades that issue.  (With the numbers from drand48() the multiplication
is exact; with 53-bit random numbers it is not.)





More information about the erlang-questions mailing list