Learn R Programming

random.cdisc.data (version 0.3.16)

rtexp: Truncated Exponential Distribution

Description

[Stable]

This generates random numbers from a truncated Exponential distribution, i.e. from X | X > l or X | X < r when X ~ Exp(rate). The advantage here is that we guarantee to return exactly n numbers and without using a loop internally. This can be derived from the quantile functions of the left- and right-truncated Exponential distributions.

Usage

rtexp(n, rate, l = NULL, r = NULL)

Value

The random numbers. If neither l nor r are provided then the usual Exponential distribution is used.

Arguments

n

(numeric)
Number of random numbers.

rate

(numeric)
Non-negative rate.

l

(numeric)
Positive left-hand truncation parameter.

r

(numeric)
Positive right-hand truncation parameter.

Examples

Run this code
x <- stats::rexp(1e6, rate = 5)
x <- x[x > 0.5]
hist(x)

y <- rtexp(1e6, rate = 5, l = 0.5)
hist(y)

z <- rtexp(1e6, rate = 5, r = 0.5)
hist(z)

Run the code above in your browser using DataLab