secrRNG: Random Number Seed

Description Random numbers in R Handling of RNG seed for simulation in package stats Use of random numbers in secr See Also Examples

Description

The use of random number seeds in secr is explained.

Random numbers in R

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 stats

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.

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

Use of random numbers in secr

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
## 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]


## End(Not run)

secr documentation built on May 14, 2018, 9:06 a.m.