Skip to contents

These functions each construct an iterator that produces random numbers of various distributions. Each one is a wrapper around a base R function.

Usage

irnorm(
  n,
  mean = 0,
  sd = 1,
  count = Inf,
  independent = !missing(seed) || !missing(kind),
  seed = NULL,
  kind = NULL,
  normal.kind = NULL,
  sample.kind = NULL
)

irbinom(
  n,
  size,
  prob,
  count = Inf,
  independent = !missing(seed) || !missing(kind),
  seed = NULL,
  kind = NULL,
  normal.kind = NULL,
  sample.kind = NULL
)

irnbinom(
  n,
  size,
  prob,
  mu,
  count = Inf,
  independent = !missing(seed) || !missing(kind),
  seed = NULL,
  kind = NULL,
  normal.kind = NULL,
  sample.kind = NULL
)

irpois(
  n,
  lambda,
  count = Inf,
  independent = !missing(seed) || !missing(kind),
  seed = NULL,
  kind = NULL,
  normal.kind = NULL,
  sample.kind = NULL
)

isample(
  x,
  size,
  replace = FALSE,
  prob = NULL,
  count = Inf,
  independent = !missing(seed) || !missing(kind),
  seed = NULL,
  kind = NULL,
  normal.kind = NULL,
  sample.kind = NULL
)

irunif(
  n,
  min = 0,
  max = 1,
  count = Inf,
  independent = !missing(seed) || !missing(kind),
  seed = NULL,
  kind = NULL,
  normal.kind = NULL,
  sample.kind = NULL
)

Arguments

n

How many samples to compute per call; see e.g. rnorm.

mean

see rnorm.

sd

see rnorm.

count

number of times that the iterator will fire. If not specified, it will fire values forever.

independent

If TRUE, this iterator will keep its own private random state, so that its output is reproducible and independent of anything else in the program; this comes at some performance cost. Default is FALSE unless seed or kind are given. If independent=TRUE but neither seed nor kind are specified, we will use the "L'Ecuyer-CMRG" generator with a seed value taken from a package-private instance of iRNGStream.

seed

A specific seed value for reproducibility. If given, independent=TRUE is implied. This can be a single number (which will be passed to set.seed(seed, kind, normal.kind, sample.kind); it can also be a vector containing a complete, valid state for .Random.seed. If the latter, arguments kind, etc. are ignored.

kind

Which random number algorithm to use; passed along to set.seed, If given, independent=TRUE is implied.

normal.kind

Passed along to set.seed.

sample.kind

Passed along to set.seed.

size

see e.g. rbinom.

prob

see e.g. rbinom.

mu

see rnbinom.

lambda

see rpois.

x

see isample.

replace

see isample.

min

see runif.

max

see runif.

Value

An iterator that is a wrapper around the corresponding random number generator function.

Details

Originally from the iterators package.

See also

If you are creating multiple independent iterators, iRNGStream will create well-separated seed values, which may help avoid spurious correlations between iterators.

Examples


# create an iterator that returns three random numbers
it <- irnorm(1, count = 3)
nextOr(it)
#> [1] 0.5809965
nextOr(it)
#> [1] 0.7681787
nextOr(it)
#> [1] 0.4637676
nextOr(it, NULL)
#> NULL

# iterators created with a specific seed will make reproducible values
it <- irunif(n=1, seed=314, kind="L'Ecuyer-CMRG")
nextOr(it) # 0.4936700
#> [1] 0.49367
nextOr(it) # 0.5103891
#> [1] 0.5103891
nextOr(it) # 0.2338745
#> [1] 0.2338745

# the iRNGStream produces a sequence of well separated seed values,
rng.seeds <- iRNGStream(313)
it1 <- isample(c(0, 1), 1, seed=nextOr(rng.seeds))
it2 <- isample(c(0, 1), 1, seed=nextOr(rng.seeds))
it3 <- isample(c(0, 1), 1, seed=nextOr(rng.seeds))
take(it1, 5, "numeric") # 0 1 0 0 1
#> [1] 0 1 0 0 1
take(it2, 5, "numeric") # 0 1 0 0 0
#> [1] 0 1 0 0 0
take(it3, 5, "numeric") # 0 0 0 1 1
#> [1] 0 0 0 1 1