Learn R Programming

simEd (version 1.0.3)

vnorm: Variate Generator for the Normal Distribution

Description

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

Usage

vnorm(n, mean = 0, sd = 1, stream = NULL, antithetic = FALSE)

Arguments

n

number of observations

mean

mean of the distribution

sd

standard deviation of the distribution

stream

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

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 normal random variates

Details

Generates random variates from the normal distribution.

Normal 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::qnorm is used to invert the uniform(0,1) variate(s). In this way, using vnorm provides a monotone and synchronized normal variate generator, although not particularly fast.

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

If mean or sd are not specified, they assume the default values of 0 and 1, respectively.

The normal distribution has density

$$f(x) = \frac{1}{\sqrt{2\pi}\sigma} e^{-(x - \mu)^2/(2 \sigma^2)}$$

for \(-\infty < x < \infty\) and \(\sigma > 0\), where \(\mu\) is the mean of the distribution and \(\sigma\) the standard deviation.

See Also

stats::rnorm, stats::runif, rstream, set.seed

Examples

Run this code
# NOT RUN {
set.seed(8675309)
vnorm(3)  # generate standard normals: inverts stats::runif using stats::qnorm

set.seed(8675309)
vnorm(3, mean = 2, sd = 1)  # inverts stats::runif using stats::qnorm

set.seed(8675309)
vnorm(3, mean = 2, sd = 1, stream = 1) # inverts rstream::rstream.sample using stats::qnorm
vnorm(3, mean = 2, sd = 1, stream = 2)

set.seed(8675309)
vnorm(1, mean = 2, sd = 1, stream = 1) # inverts rstream::rstream.sample using stats::qnorm
vnorm(1, mean = 2, sd = 1, stream = 2)
vnorm(1, mean = 2, sd = 1, stream = 1)
vnorm(1, mean = 2, sd = 1, stream = 2)
vnorm(1, mean = 2, sd = 1, stream = 1)
vnorm(1, mean = 2, sd = 1, stream = 2)

set.seed(8675309)
variates <- vnorm(1000, mean = 10, sd = 2, stream = 1)
set.seed(8675309)
variates <- vnorm(1000, mean = 10, sd = 2, stream = 1, antithetic = TRUE)
# }

Run the code above in your browser using DataLab