# Fixing random seed

It is desirable that the default behavior of Dynare be fully deterministic: two runs of the same *.mod file on the same installation should provide the same results. Introduced in commit db54c2d76ec8ac6a42c0cbf99bc394f7644f2a85 (git master), there is a new matlab routine for setting the seed: set_dynare_seed.m. This routine is called in global_initialization.m, so that each instance of dynare is initialized with the same seed (and should give exactly the same results). This routine can also be called in a mod file, depending on the version of matlab the user can set the seed and (with ver('matlab')>=7.7) choose the algorithm used to generate uniform variates (see matlab documentation or the output of the command RandStream.list).

### List of commands or functions that use a random generator

- stoch_simul (csolve simult_ + currently call to dynare_simul_ uses a fixed seed)
- estimation
- mode optimization
- Metropolis iterations
- subsampling for posterior statistics

- osr optimization
- steady (csolve)
- ramsey_policy (csolve + simult_)
- Markov switching
- Global Sensitivity Analysis

### set_dynare_seed(a,b)

- With two arguments, the first argument sets the random generator type and the second argument sets the seed
with one argument, if the argument is numeric, it sets the seed of the random generator of the default type (depends on Matlab version). If the single argument is not numeric, and not

`default`or`reset`an error is triggeredThe function sets options_.DynareRandomStreams.algo and options_.DynareRandomStreams.seed

- In older versions, Matlab used two distinct random generators for uniformly distributed and for normally distributed variables. Dynare uses the same seed for both generators
- Octave has only one type of random generator, Mersenne Twister, the same algorithm as the default in recent versions of Matlab

### get_dynare_random_generator_state()

- this function returns two vectors describing the current state of the random generators (one for uniformly, one for normally distributed variables)
- this function handles automatically the different versions of random generators available in Matlab * as noted, above, in recent Matlab versions, there is only one random generator and in that case the two vectors are identical.

### set_dynare_random_generator_state(state_u,state_n)

- uses both arguments to set the state of the random generators. When using the current mechanism provided by recent versions of Matlab, both vectors must be identical.
- this function handles automatically the different versions of random generators available in Matlab

### Metropolis

- since August 29, 2012, the random generator is reseeded with a different seed before the computation of each Metropolis chain.
the current mechanism, using only the default random stream, will need to be revised if we use

`parfor`to compute in parallel the various chains.the state of the random generator is stored in

`record.Seeds(j).Unifor`and`record.Seeds(j).Normal`, where`j`is the index of the Metropolis chain. Note that the field name`Seeds`is misleading because it stores the state vector and not the original seed. We keep it that way for backward compatibility.- this mechanism will fail on a heterogenous cluster where the random generator used by the master is not available on the slave. In this case, we only re-seed each Metropolis chain but don't control the state.
in heterogenous clusters, options

`mh_recover`and`load_mh_file`will not necessarily work.- starting with commit 692acc66a7b52dc589f7a92ee4dbaeebe7b0138e (git master), the seed used to generate the starting values for multiple MCMC chains is also fixed.
starting with commit a55f00073be45d563ec251324344ad8522c397ba (git master), the respective seed at the end of the respective MCMC chain is saved in the mh-files for use via

`mh_recover`