Learn R Programming

secr (version 4.6.9)

secrRNG: Random Number Seed

Description

The use of random number seeds in secr is explained.

Arguments

Random numbers in <span style="R">R</span>

R provides several kinds of random number generator (RNG) in the base package (see RNG). These are used both explicitly, in functions such as runif and rnorm, and implicitly (sample).

A seed suitable for any kind of RNG is held in a vector of 626 integers named .Random.seed. The vector is not to be modified directly by users. Instead, to start a reproducible stream of random numbers, the user calls set.seed with a single non-null integer argument. This has the effect of initialising .Random.seed. The value of .Random.seed may nevertheless be stored and restored to reset the RNG state.

set.seed with a NULL argument initialises .Random.seed to an indeterminate (time- and process-dependent) value. The same happens if a random number function is called before .Random.seed has been set.

Handling of RNG seed for simulation in package <span class="pkg">stats</span>

The `official' approach to setting and storing the RNG seed is shown in code and documentation for the generic function simulate in the stats package.

  • The generic has argument `seed' with default NULL.

  • If `seed' is non-null then set.seed is called.

  • The returned value has an attribute ``seed'' whose value is either (i) if specified, the integer value of the `seed' argument (with its own attribute ``kind'' from RNGkind), or (ii) the original vector .Random.seed.

  • On exit the RNG state in .Random.seed is reset to the value that applied when the function was called.

For NULL seed input, the saved RNGstate may be used to reset .Random.seed (see Examples).

Use of random numbers in <span class="pkg">secr</span>

Many functions in secr call on random numbers, sometimes in unexpected places. For example autoini selects a random sample to thin points and speed computation. In most functions there is no provision for direct control of the random number state: users won't usually care, and if they do then set.seed may be called for the particular R session.

However, control of the RNG seed is required for reproducible data generation in simulation functions. These functions typically have a `seed' argument that is used internally in a call to set.seed. Handling of seeds in the simulation functions of secr largely follows stats::simulate as described in the preceding section.

The relevant functions are --

FunctionDefaultSaved attributeNote
randomHabitatNULLseed or RNGstatesecr.test
NULLseed or RNGstatecalls and retains seed from simulate.secrsim.capthist
NULLseed or RNGstatesim.resightNULL
seed or RNGstateSeed may be passed in ... argumentsim.popnNULL
seed or RNGstatesim.secrNULLseed or RNGstate
simulate.secrNULLseed or RNGstateS3 method called by sim.secr

Setting seed = NULL in any of these functions has the effect of continuing the existing random number stream; it is not the same as calling set.seed(NULL).

Parallel processing

Two models are used for parallel processing in secr, corresponding to multi-threading with package RcppParallel (e.g. secr.fit) and parallel cores in package parallel (e.g. chat.nk).

In the parallel model the L'Ecuyer pseudorandom generator is used to provide a separate random number stream for each core (see clusterSetRNGStream).

When using Rcpp the state of the random number generator is set in C++ with the call RNGScope scope;that automatically resets the state of the generator on exit (Eddelbuettel 2013 p. 115).

Random number streams in separate RcppParallel threads are (probably) not independent. Thus there are potential issues with RNG calls in multi-threaded code. However, in secr 4.0 all RNG calls in C++ code are outside multi-threaded contexts, with the exception of simulations allowing for overdispersion in mark--resight estimates (Rcpp exported function sightingchatcpp). The implications for mark-resight estimates have not been explored, and it is unclear whether more elaborate solutions are needed.

References

Eddelbuettel, D. 2013. Seamless R and C++ integration with Rcpp. Springer.

See Also

Parallel, set.seed, simulate, sim.capthist, sim.popn, sim.resight, secr.test, simulate.secr

Examples

Run this code

if (FALSE) {

lmfit <- lm(speed ~ dist, data = cars)

## 1. NULL seed
r1 <- simulate(lmfit, seed = NULL)
r2 <- simulate(lmfit, seed = NULL)
## restore RNGstate, assuming RNGkind unchanged
.Random.seed <- attr(r1, "seed")
r3 <- simulate(lmfit, seed = NULL)
r1[1:6,1]
r2[1:6,1]
r3[1:6,1]

## 2. explicit seed
r4 <- simulate(lmfit, seed = 123)
r5 <- simulate(lmfit, seed = attr(r4, "seed"))
r4[1:6,1]
r5[1:6,1]

}

Run the code above in your browser using DataLab