Learn R Programming

soundgen (version 2.7.2)

rnorm_truncated2: Random draw from a truncated normal distribution

Description

A simplified version of rnorm_truncated, in which values outside the bounds are simply reset to the low/high bounds. The shape of the resulting distribution is no longer Gaussian, but this is obviously much faster. Unlike in rnorm_truncated, "low" and "high" should be scalars, not vectors (ie static boundaries).

Usage

rnorm_truncated2(
  n = 1,
  mean = 0,
  sd = 1,
  low = NULL,
  high = NULL,
  roundToInteger = FALSE,
  invalidArgAction = c("adjust", "abort", "ignore")[1]
)

Value

A vector of length n.

Arguments

n

the number of values to return

mean

the mean of the normal distribution from which values are generated (vector of length 1 or n)

sd

the standard deviation of the normal distribution from which values are generated (vector of length 1 or n)

low, high

exclusive lower and upper bounds (both of length 1)

roundToInteger

boolean vector of length 1 or n. If TRUE, the corresponding value is rounded to the nearest integer.

Examples

Run this code
hist(soundgen:::rnorm_truncated2(n = 100, mean = 10, sd = 5, low = 7, high = NULL,
  roundToInteger = c(TRUE, FALSE, FALSE)))
hist(soundgen:::rnorm_truncated2(n = 100, mean = c(10, 50, 100), sd = c(5, 0, 20),
  roundToInteger = TRUE)) # vectorized
# in case of conflicts between mean and bounds, either sample at random
# between the boundaries...
hist(soundgen:::rnorm_truncated2(n = 100, mean = 10, sd = .1,
  low = 10, high = 15, invalidArgAction = 'adjust'))
#... or ignore the boundaries
hist(soundgen:::rnorm_truncated2(n = 100, mean = 10, sd = .1,
  low = 15, high = 100, invalidArgAction = 'ignore'))
soundgen:::rnorm_truncated2(n = 6, mean = c(0, 0, 0, 0, 0, 3),
  sd = .05, low = 0, high = 6)

Run the code above in your browser using DataLab