Skip to content

Cover more values in the [0; 1) range by “random”.

Rika requested to merge runewalsh/source:randmant into main

Let random : extended always have double mantissa worth of random bits. Read this Julia thread, look at the Zig implementation it mentions, and try this demo RandomTest.pas:

trunk random:
min nonzero =  2.3283064365386963E-010
               0 exp 01111011111 mant 0000000000000000000000000000000000000000000000000000
max =          9.9999999976716936E-001
               0 exp 01111111110 mant 1111111111111111111111111111111000000000000000000000
time = 32.1 s

trunk random, but 52 bits:
min nonzero =  2.4187984948298435E-011
               0 exp 01111011011 mant 1010100110000101000000000000000000000000000000000000
max =          9.9999999994109356E-001
               0 exp 01111111110 mant 1111111111111111111111111111111101111110011101101010
time = 54.8 s

this MR:
min nonzero =  2.5054744884384805E-010
               0 exp 01111011111 mant 0001001101111010110101100101101100100100101011110110
max =          9.9999999998013167E-001
               0 exp 01111111110 mant 1111111111111111111111111111111111010100010011110010
time = 58.0 s

As a side effect, it prevents random from returning 0 (0 theoretically has 2−1022−52 probability but with 2128 RNG states 2−1022−52 is impossible without qualifications), which might be (eg if you take logarithms) or not be (eg if you... hm...) a good thing.

Merge request reports