Debian Codegraph @

docs_sphinx/advanced/random.rst

raw
1 Random numbers
2 ==============
3
4 Brian provides two basic functions to generate random numbers that can be used in model code and equations: ``rand()``,
5 to generate uniformly generated random numbers between 0 and 1, and ``randn()``, to generate random numbers from a
6 standard normal distribution (i.e. normally distributed numbers with a mean of 0 and a standard deviation of 1). All
7 other stochastic elements of a simulation (probabilistic connections, Poisson-distributed input generated by
8 `PoissonGroup` or `PoissonInput`, differential equations using the noise term ``xi``, ...) will internally make use of
9 these two basic functions.
10
11 For :ref:`runtime`, random numbers are generated by `numpy.random.rand` and `numpy.random.randn` respectively, which
12 uses a `Mersenne-Twister <https://en.wikipedia.org/wiki/Mersenne_Twister>`_ pseudorandom number generator. When the
13 ``numpy`` code generation target is used, these functions are called directly, but for ``cython``, Brian
14 uses a internal buffers for uniformly and normally distributed random numbers and calls the numpy functions whenever
15 all numbers from this buffer have been used. This avoids the overhead of switching between C code and Python code for
16 each random number. For :ref:`cpp_standalone`, the random number generation is based on "randomkit", the same
17 Mersenne-Twister implementation that is used by numpy. The source code of this implementation will be included in every
18 generated standalone project.
19
20 .. _seeding_and_reproducibility:
21
22 Seeding and reproducibility
23 ---------------------------
24 Runtime mode
25 ~~~~~~~~~~~~
26 As explained above, :ref:`runtime` makes use of numpy's random number generator. In principle, using `numpy.random.seed`
27 therefore permits reproducing a stream of random numbers. However, for ``cython``, Brian's buffer
28 complicates the matter a bit: if a simulation sets numpy's seed, uses 10000 random numbers, and then resets the seed,
29 the following 10000 random numbers (assuming the current size of the buffer) will come out of the pre-generated buffer
30 before numpy's random number generation functions are called again and take into account the seed set by the user.
31 Instead, users should use the `seed` function provided by Brian 2 itself, this will take care of setting numpy's random
32 seed *and* empty Brian's internal buffers. This function also has the advantage that it will continue to work when the
33 simulation is switched to standalone code generation (see below). Note that random numbers are not guaranteed to be
34 reproducible across different code generation targets or different versions of Brian, especially if several sources of
35 randomness are used in the same `CodeObject` (e.g. two noise variables in the equations of a `NeuronGroup`). This is
36 because Brian does not guarantee the order of certain operations (e.g. should it first generate all random numbers for
37 the first noise variable for all neurons, followed by the random numbers for the second noise variable for all neurons
38 or rather first the random numbers for all noice variables of the first neuron, then for the second neuron, etc.) Since
39 all random numbers are coming from the same stream of random numbers, the order of getting the numbers out of this
40 stream matter.
41
42 Standalone mode
43 ~~~~~~~~~~~~~~~
44 For :ref:`cpp_standalone`, Brian's `seed` function will insert code to set the random number generator seed into the
45 generated code. The code will be generated at the position where the `seed` call was made, allowing detailed control
46 over the seeding. For example the following code would generate identical initial conditions every time it is run, but
47 the noise generated by the ``xi`` variable would differ::
48
49 G = NeuronGroup(10, 'dv/dt = -v/(10*ms) + 0.1*xi/sqrt(ms) : 1')
50 seed(4321)
51 G.v = 'rand()'
52 seed()
53 run(100*ms)
54
55 .. note::
56
57 In standalone mode, `seed` will not set numpy's random number generator. If you use random numbers in the Python
58 script itself (e.g. to generate a list of synaptic connections that will be passed to the standalone code as a
59 pre-calculated array), then you have to explicitly call `numpy.random.seed` yourself to make these random numbers
60 reproducible.
61
62 .. note::
63
64 Seeding *should* lead to reproducible random numbers even when using OpenMP with multiple threads (for repeated
65 simulations with the same number of threads), but this has not been rigorously tested. Use at your own risk.