Learn R Programming

simEd (version 1.0.3)

vunif: Variate Generator for the Uniform Distribution

Description

Generates random variates from the uniform distribution, with options for independent streams and antithetic variates.

Usage

vunif(n, min = 0, max = 1, stream = NULL, antithetic = FALSE)

Arguments

n

number of observations

min, max

lower and upper limits of the distribution (must be finite)

stream

if NULL (default), uses stats::runif to generate uniform variates to invert via stats::qunif; otherwise, an integer in 1:25 indicates the rstream stream from which to generate uniform variates to invert via stats::qunif

antithetic

if FALSE (default), inverts \(u\) = uniform(0,1) variate(s) generated via either stats::runif or rstream::rstream.sample; otherwise, uses \(1 - u\)

Value

A vector of uniform random variates.

Details

Generates random variates from the uniform distribution.

Uniform variates are generated by inverting uniform(0,1) variates produced either by stats::runif (if stream is NULL) or by rstream::rstream.sample (if stream is not NULL). In either case, stats::qunif is used to invert the uniform(0,1) variate(s). In this way, using vunif provides a monotone and synchronized uniform variate generator, although not particularly fast.

The stream indicated must be an integer between 1 and 25 inclusive.

The uniform distribution has density

$$f(x) = \frac{1}{max-min}$$

for \(min \le x \le max\).

See Also

stats::runif, rstream, set.seed

Examples

Run this code
# NOT RUN {
set.seed(8675309)
vunif(3)  # inverts stats::runif using stats::qunif

set.seed(8675309)
vunif(3, min = -2, max = 2)  # inverts stats::runif using stats::qunif

set.seed(8675309)
vunif(3, -2, 2, stream = 1) # inverts rstream::rstream.sample using stats::qunif
vunif(3, -2, 2, stream = 2)

set.seed(8675309)
vunif(1, -2, 2, stream = 1) # inverts rstream::rstream.sample using stats::qunif
vunif(1, -2, 2, stream = 2)
vunif(1, -2, 2, stream = 1)
vunif(1, -2, 2, stream = 2)
vunif(1, -2, 2, stream = 1)
vunif(1, -2, 2, stream = 2)

set.seed(8675309)
variates <- vunif(1000, min = 0, max = 10, stream = 1)
set.seed(8675309)
variates <- vunif(1000, min = 0, max = 10, stream = 1, antithetic = TRUE)
# }

Run the code above in your browser using DataLab