R/pick_random_event.R

#' Takes a probability distribution of events and returns which event happened based on
#' those probabilities
#'
#' @param distribution A vector p where p[i] denotes the relative probability of event i
#' @return An index i (to denote that event i took place)
#' @export
#'
pick_random_event <- function(distribution)
{
  distribution <- cumsum(distribution/sum(distribution))
  r <- runif(1)
  return(1 + sum(distribution < r))
}
sams25/rcombinator_old documentation built on May 28, 2019, 8:40 a.m.