Learn R Programming

secr (version 3.2.1)

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. Exceptions are ip.secr and par.secr.fit; both allow control of the seed, but do not save it.

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 --

Function Default Saved attribute Note
randomHabitat NULL seed or RNGstate secr.test
NULL seed or RNGstate calls and retains seed from simulate.secr sim.capthist
NULL seed or RNGstate sim.resight NULL
seed or RNGstate Seed may be passed in … argument sim.popn NULL
seed or RNGstate sim.secr NULL seed or RNGstate
simulate.secr NULL seed or RNGstate S3 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).

When several cores are used for parallel processing (ncores > 1) the L'Ecuyer pseudorandom generator is used to provide a separate random number stream for each core (see clusterSetRNGStream).

See Also

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

Examples

Run this code
# NOT RUN {
# }
# NOT RUN {
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]

# }
# NOT RUN {
# }

Run the code above in your browser using DataLab