rtrunc: Random Truncated Distributions

View source: R/rtrunc.R

rtruncR Documentation

Random Truncated Distributions

Description

Provides samples from classical R distributions and ‘⁠mc2d⁠’ specific distributions truncated between ‘⁠linf⁠’ (excluded) and ‘⁠lsup⁠’ (included).

Usage

rtrunc(distr = runif, n, linf = -Inf, lsup = Inf, ...)

Arguments

distr

A function providing random data or its name as character. The function ‘⁠rdistr⁠’ should have a ‘⁠qdistr⁠’ form (with argument ‘⁠p⁠’) and a ‘⁠pdistr⁠’ form (with argument ‘⁠q⁠’). Example: ‘⁠rnorm⁠’ (has a ‘⁠qnorm⁠’ and a ‘⁠pnorm⁠’ form), ‘⁠rbeta⁠’, ‘⁠rbinom⁠’, ‘⁠rgamma⁠’, ...

n

the size of the sample.

linf

a vector of lower bounds.

lsup

a vector of upper bounds, with ‘⁠lsup > linf⁠’ (strictly).

...

all arguments to be passed to ‘⁠pdistr⁠’ and ‘⁠qdistr⁠’.

Details

The function 1) evaluates the ‘⁠p⁠’ values corresponding to ‘⁠linf⁠’ and ‘⁠lsup⁠’ using ‘⁠pdistr⁠’; 2) samples ‘⁠n⁠’ values using ‘⁠runif(n, min=pinf, max=psup)⁠’, and 3) takes the ‘⁠n⁠’ corresponding quantiles from the specified distribution using ‘⁠qdistr⁠’.

All distributions (but sample) implemented in the stats library could be used. The arguments in ... should be named. Do not use ‘⁠log⁠’, ‘⁠log.p⁠’ or ‘⁠lower.tail⁠’.

For discrete distributions, ‘⁠rtrunc⁠’ samples within ‘⁠(linf, lsup]⁠’.

Warning: The method is flexible, but can lead to problems linked to rounding errors in some extreme situations. The function checks that the values are in the expected range and returns an error if not.

Value

A vector of ‘⁠n⁠’ values.

Examples

rtrunc("rnorm", n = 10, linf = 0)
range(rtrunc(rnorm, n = 1000, linf = 3, lsup = 5, sd = 10))
## Discrete distributions
range(rtrunc(rpois, 1000, linf = 2, lsup = 4, lambda = 1))

mc2d documentation built on Oct. 23, 2026, 1:06 a.m.