Random number generator interface

Since so many plausible random number generators exist, ideally one can vary the algorithm used and and verify that the results of a program are qualitatively unchanged. There was a 1995 paper about this:

A Fortran 90 interface to random number generation

by Michael Hennecke

Abstract
A Fortran 90 MODULE has been implemented which is used to replace the intrinsic random number generation subroutines RANDOM_NUMBER and RANDOM_SEED with a user-defined generator. It is shown how the new features of Fortran 90 can be used to define a set of user subroutines that behave as the intrinsic procedures do, and thus provide a convenient way to interface to arbitrary random number generators without the need to change the calls to the random number generator in the user program. Implementation examples demonstrate how to call an “external” generator as well as how to integrate an algorithm into the module.

The ideas of this paper were implemented in module_random.f90 of statpack. Below is a snippet. Maybe the RNG module in stdlib should be written so that there is similar flexibility, with the user being able to easily add her own favorite RNG algorithm.

!   THE PRIVATE INTEGER VARIABLE method SETS THE RANDOM NUMBER GENERATOR USED FOR SUBSEQUENT
!   CALLS OF THE rand_number, rand_integer32, rand_integer31, random_number_, random_integer32_,
!   random_integer31_ ROUTINES:
!
!     method = 1  : THE Marsaglia's KISS RNG IS USED.
!     method = 2  : THE FAST Marsaglia's KISS RNG, WHICH USES ONLY
!                   ADD, SHIFT, EXCLUSIVE-OR AND "AND" OPERATIONS,
!                   IS USED (SAME RESULTS AS THE C VERSION).
!     method = 3  : THE L'Ecuyer's LFSR113 RNG IS USED.
!     method = 4  : THE MERSENNE TWISTER MT19937 RNG IS USED.
!     method = 5  : THE MERSENNE TWISTER MEMT19937-II RNG IS USED.
!     method = 6  : THE EXTENDED VERSION OF THE Marsaglia's KISS RNG IS USED.
!     method = 7  : THE EXTENDED VERSION OF THE FAST Marsaglia's KISS RNG IS USED.
!     method = 8  : THE EXTENDED VERSION OF THE L'Ecuyer's LFSR113 RNG IS USED.
!     method = 9  : THE EXTENDED VERSION OF THE MERSENNE TWISTER MT19937 RNG IS USED.
!     method = 10 : THE EXTENDED VERSION OF THE MERSENNE TWISTER MEMT19937-II RNG IS USED.
!
!   IN ORDER TO CHANGE THE RNG, YOU MUST USE A CALL TO THE random_seed_ SUBROUTINE WITH
!   THE OPTIONAL alg ARGUMENT SET TO 1, 2, 3, 4, 5, 6, 7, 8, 9 OR 10. NOTE THAT FOR RANDOM INTEGER
!   GENERATION, METHODS 6, 7, 8, 9 AND 10 ARE EXACTLY IDENTICAL TO METHODS 1, 2, 3, 4 AND 5,
!   RESPECTIVELY. THEY DIFFER ONLY FOR RANDOM REAL GENERATION.
!
!   THE PRIVATE INTEGER VARIABLE method STORED THE METHOD CURRENTLY IN USE, num_seeds
!   STORED THE NUMBER OF SEEDS USED BY THIS METHOD AND seed_size IS THE CORRESPONDING SIZE
!   OF THE SEED VECTOR AS RETURNED BY A CALL TO random_seed_ SUBROUTINE WITH ARGUMENT size.
!
!   THE DEFAULT METHOD CAN BE SAFELY CHANGED HERE, BETWEEN 1, 2 AND 3.
!   NOTE THAT THE OTHER METHODS CANNOT BE USED SAFELY AS THE DEFAULT METHOD.
!   FURTHERMORE IF 32-BIT INTEGERS ARE NOT AVAILABLE THE DEFAULT METHOD MUST
!   BE 3, BECAUSE THE KISS RNGS WORK PROPERLY ONLY WITH 32-BIT INTEGERS.
2 Likes