R/random-generator-function.R

# random number generators  
# obtained from U(0,1) generated by runif(1,0,1)

ran.unif <- function(a,b){
 u <- runif(1,0,1)  # generate uniform U(0,1)
 x <- (b-a)*u+a     # apply transform
 return(x)	    # return the calculated value
}

# exponential
ran.exp <- function(rate){
   u <- runif(1,0,1) # generate uniform U(0,1)
   x <- (1/rate)*(-log(u))
  return(x)
}

# standard normal
ran.norm <- function( ){
 # generate two values from uniform
 u1 <- runif(1,0,1); u2 <- runif(1,0,1)
 z <- cos(2*pi*u1)*sqrt(-2*log(u2))
 return(z)
}

 

Try the seem package in your browser

Any scripts or data that you put into this service are public.

seem documentation built on April 14, 2017, 9:12 p.m.