Skip to content

Commit 763a7b3

Browse files
committed
Update documentation
1 parent 7b44f40 commit 763a7b3

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

doc/modules/algorithm.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,12 @@ shuffle
206206
-------
207207

208208
The "shuffle" subroutine depends on the :ref:`modules_random` module so that it
209-
can use the supported random number generator to randomly reorder an array.
209+
can use the default random number generator to randomly reorder an array.
210210

211211
Example::
212212

213213
use flc_algorithm, only : shuffle
214-
use flc_random, only : Engine
214+
use flc_random, only : Engine => MersenneEngine4
215215
implicit none
216216
integer :: i
217217
integer, dimension(8) :: iarr = (/ ((i), i = -4, 3) /)

doc/modules/random.rst

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,67 @@ Random
99
******
1010

1111
The ``flc_random`` module contains advanced pseudo-random number generation
12-
from the `<random>`_ C++ header. Currently
13-
random number generation is hardwired to the ``std::mt19937_64`` 64-bit
14-
Mersenne Twister by Matsumoto and Nishimura.
12+
from the `<random>`_ C++ header.
1513

1614
The C++11 way of generating random numbers takes some getting used to. Rather
1715
than having a single global function that returns a random real number in the
1816
range :math:`[0,1)`, C++11 has independent *engine* objects that generate
1917
streams of random bits. Different *distribution* objects convert those bits
2018
into samples of a distribution.
2119

20+
Although C++11 defines a dizzying variety of random number engines,
21+
``flc_random`` wraps just two: the 32- and 64-bit `Mersenne Twister`
22+
algorithms. The 32-bit version (``MersenneEngine4``) is currently the only
23+
engine type that can be used with distributions and algorithms.
24+
2225
Flibcpp wraps distribution objects as independent subroutines. Each subroutine
2326
accepts the constructor parameters of the distribution, the random engine, and
2427
a target Fortran array to be filled with random numbers.
2528

2629
Generating an array with 10 normally-distributed reals with a mean of 8 and a
2730
standard deviation of 2 is done as such::
2831

29-
use flc_random, only : Engine, normal_distribution
32+
use flc_random, only : Engine => MersenneEngine4, normal_distribution
3033
real(C_DOUBLE), dimension(20) :: arr
3134
type(Engine) :: rng
3235

3336
rng = Engine()
3437
call normal_distribution(8.0d0, 2.0d0, rng, arr)
38+
call rng%release()
3539

3640
.. _<random> : https://en.cppreference.com/w/cpp/numeric/random
41+
.. _Mersenne Twister : https://en.wikipedia.org/wiki/Mersenne_Twister
42+
43+
Engines
44+
=======
45+
46+
The two Mersenne twister engines in ``flc_random`` return different-sized
47+
integers per call:
48+
49+
- ``MersenneEngine4``: each invocation returns a 32-bit ``integer(4)``
50+
- ``MersenneEngine8``: each invocation returns a 64-bit ``integer(8)``
51+
52+
Engines can be constructed using one of two interface functions: the
53+
argument-free ``MersenneEngine4()`` uses the default seed, and the engine takes
54+
a single argument ``MersenneEngine4(1234567)`` with the seed. Alternatively,
55+
the seed can be set (or reset) using the ``seed()`` type-bound procedure.
3756

38-
Engine
39-
======
57+
Generally, engines are used with distributions (described below). However, if
58+
necessary, individual randomly generated values can be obtained by calling
59+
the ``next()`` type-bound procedure.
4060

41-
The ``Engine`` is a derived type that wraps the Mersenne Twister PRNG engine.
42-
Its interface is::
61+
.. warning:: C++ generates *unsigned* integers with entropy in every bit. This
62+
means that the integers obtained from ``engine%next()``, reinterpreted as
63+
signed Fortran integers, may be negative.
4364

44-
type, public :: Engine
45-
contains
46-
procedure :: seed => swigf_Engine_seed
47-
procedure :: discard => swigf_Engine_discard
48-
procedure :: release => delete_Engine
49-
end type Engine
50-
interface Engine
51-
module procedure new_Engine
52-
end interface
65+
In most cases, the default distribution-compatible ``MersenneEngine4`` should
66+
be used, since the distributions described below require it.
5367

5468
Distributions
5569
=============
5670

5771
Distributions produce numerical values from the random bitstream provided by
58-
the Engine.
72+
an RNG engine.
5973

6074
normal_distribution
6175
-------------------

0 commit comments

Comments
 (0)