lundi 3 février 2020

Generating random integer and real numbers in a given range

According the man page of getNext in the PCGRandom module, we can generate random numbers in a given range, for example:

use Random;

var rng1 = new owned RandomStream( eltType= real, seed= 100 );
var rng2 = new owned RandomStream( eltType= int,  seed= 100 );

for i in 1..5 do
    writeln( rng1.getNext( min= 3.0, max= 5.0 ) );

writeln();
for i in 1..5 do
    writeln( rng2.getNext( min= 20, max= 80 ) );

which gives (with chpl-1.20.0):

4.50371
4.85573
4.2246
4.84289
3.63607

36
57
79
39
57

Here, I noticed that the man page gives the following notes for both the integer and real-number cases:

For integers, this class uses a strategy for generating a value in a particular range that has not been subject to rigorous study and may have statistical problems.

For real numbers, this class generates a random value in [max, min] by computing a random value in [0,1] and scaling and shifting that value. Note that not all possible floating point values in the interval [min, max] can be constructed in this way.

(where I used italics for emphasis). For real numbers, is this related to the so-called "density of floating-point number", e.g. asked in this page)? Also, for integers, is there some case that we need to be careful even for "typical" use? (here, "typical" means, e.g., a generation of 10**8 random integers distributed approximately flat in a given range.)

FYI, my "use case" is not something like rigorous quality tests for random numbers, but just typical Monte Carlo calculations (e.g., selecting random sites on a cubic lattice).




Aucun commentaire:

Enregistrer un commentaire