Generating Values from Continuous Random Variables For any random variable, X, with probability distribution f(x), generating values from f(x) typically involves the following steps: (1) Generate a value from U[0,1), say r. Treat this as a percentile. (2) Determine the value on the distribution that is at the r-th percentile. This is the value we generate. In order to know what the r-th percentile is, we need to know the cumulative distribution of X. The cumulative distribution of X is defined as F ( x) P{ X x} p( y ) y x Consider the probability distribution: p(x) 0.5 0.3 0.2 X 1 2 3 Then X 1 2 3 p(x) 0.5 0.3 0.2 F(x) 0.5 0.8 1.0 To generate values from this distribution, we first generate a uniform [0,1) random variable r. If r < .5, let X = 1; if .5 < r < .8, let X = 2; otherwise X = 3. If r = .6, the question is, at what value does the 60th percentile occur? We can see that the 60th percentile happens at X = 2. We can generate values from any distribution, including continuous ones, using the same procedure. (1) (2) (3) Generate a U[0,1) random variate, r. Determine F(x). Set r = F(x) and solve for x. Performing step (3) allows us to determine a function x = F-1(r) from which we can generate values of x. Example 1: Generate values uniformly distributed on [0,5]. Note that 1 f ( x) 5 0 0 x5 otherwise f(x) = 1/5 0 5 Then (1) F ( x) 0 ( 1 )dy 5 x x y x 0 x 50 5 5 5 What does the graph of F(x) look like? (2) Let our U[0,1) be the values of r. (3) r F ( x) x 5 x 5r . Does this make intuitive sense? Example 2: 2 x 0 x 1 f ( x) 0 otherwise 2 0 1 (1) Determine F(x). For 0 < x < 1, F ( x) 0 f ( y )dy x 0 2 ydy x y2 x2 What does the graph of F(x) look like? x 0 (2) Let our U[0,1) random number be r. (3) Set r = F(x). So r = x2, and xr 1 2 By going to EXCEL and using rand(), we will take its square root. The values thus generated will appear to be coming from f(x). In-class exercise: (A) How should be generate values for a random variable that is uniform on [3,11]? (B) Given the distribution 3 2 x f ( x) 8 0 0 x2 otherwise (1) Find F(x). (2) Set r = F(x) and solve for x. Example 3: (Arena does this automatically) Generate values from an exponential distribution with parameter λ. (1) F ( x) 1 e x , x 0. (2) Let r = U[0,1). (3) Set r 1 e x . Then 1 r e x ln( 1 r ) x 1 x ln( 1 r ) Example 4: Generate values from a triangular distribution with parameters a, b, and m. Using these steps, you can show that ma ba ma r ba r x a (m a)(b a)r x b (b m)(b a)(1 r ) These formulae were used to perform our PERT simulation in the file Intro Simulations. EXCEL Macros EXCEL does provide a simpler way of generating values from several wellknown distributions. Select Tools/Data Analysis/Random Number Generation and choose OK. The macro allows you to choose from five specific distributions (Uniform, Normal, Bernoulli, Binomial, and Poisson) and two general distributions (Patterned and Discrete). This leaves out all other continuous distributions (Exponential, Gamma, Weibull, t, Phase, χ2, F, Erlang, Triangular, Beta, just to name a few), but the approach used in this handout can still be used in those cases. The primary difference between using rand() and Tools/Data Analysis/Random Number Generation is that the former will resample whenever a recalculation is performed (meaning all of the time!), while the latter generates a static value of the random variable. The former is useful, then, when you wish to resample by pressing F9 and observing the behavior of output.