Description
EDIT: An updated proposal is available below.
There are a few suggestions to have pseudorandom number generators (PRNGs) in the library, e.g., #1 (comment) and #104 (comment). Focusing on the uniform floating-point PRNG [0.0, 1.0), we have already had the intrinsic subroutine, random_number
. The first question is whether we should have custom PRNGs of the uniform distribution. Or, we should concentrate on derived PRNGs like non-uniform distributions or integer PRNGs. I think it is still worth developing custom PRNGs because of better flexibility and performance.
With the custom PRNGs, we have many more questions.
- Should we have various generators (e.g., integer numbers) in addition to a floating-point PRNG [0.0, 1.0)?
- Should it support different algorithms of PRNG, or can we provide a single algorithm?
- If true: Should it optionally call the intrinsic subroutine?
- If true: Should the seed (state) be global, private, or both?
- If global, Should it be thread-safe?
The decision defines a design of API. A possible API is the same as the intrinsic subroutines. The subroutine name in the below example is just a placeholder.
! just arbitrary precision here
type(random_number_generator) :: rng
real(dp) :: harvest
integer :: seed(2)
! for local state as derived type
seed = [1,2]
call pseudorandom_seed(rng,put=seed)
call pseudorandom_number(rng,harvest)
! for global state as saved seed
call pseudorandom_seed(put=seed)
call pseudorandom_number(harvest)
Please tell me what you think about it.
- R: https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/Random
- Python: https://docs.python.org/3/library/random.html
- NumPy: https://docs.scipy.org/doc/numpy-1.15.0/reference/routines.random.html
- Matlab: https://www.mathworks.com/help/matlab/random-number-generation.html
- Octave: https://octave.org/doc/v4.0.3/Special-Utility-Matrices.html#Special-Utility-Matrices
- Julia: https://docs.julialang.org/en/v1/stdlib/Random/
- C++: https://en.cppreference.com/w/cpp/numeric/random
- Rust: https://rust-num.github.io/num/rand/index.html#thread-local-rng
- Go: https://golang.org/pkg/math/rand/
- NAG: https://www.nag.co.uk/numeric/fl/nagdoc_fl24/html/g05/g05conts.html
- MKL: https://software.intel.com/en-us/mkl-developer-reference-fortran-random-number-generators
- IMSL: https://docs.roguewave.com/imsl/fortran/6.0/stat/default.htm?turl=rnun.htm
- GSL: https://www.gnu.org/software/gsl/doc/html/rng.html