R/simulation.R

Defines functions rsim

Documented in rsim

#' Simulate n random numbers from an rv.
#'
#' @param x a random variable
#' @param n number of simulations
#' @export
#' @examples
#' coin <- rv(c(-1, 1))
#' rsim(coin, 100)
#' mean(rsim(coin, 100))
#' mean(rsim(coin, 1000))
rsim <- function(x, n) {
  stopifnot(is.rv(x))
  sample(as.vector(x), n, prob = probs(x), replace = TRUE)
}
hadley/rv2 documentation built on May 17, 2019, 12:16 p.m.