We often do not have a simple probability distribution to describe the distribution of outcomes in many engineering applications. For example, the probability that a circuit. consisting of many components will fail is a function of the number and the .age’of the components, but we often cannot obtain a function to describe . the failure probability. In such cases engineers often resort to simulation to make predictions. The simulation program is executed many times, using a random set of numbers to represent the failure of one or more components, and the results are used to estimate the desired probability.

**Uniformly Distributed Numbers**

In a sequence of uniformly distributed random numbers, all values within a given interval are equally likely to occur. The MATLAB function rand generates random numbers uniformly distributed over the interval [0,1]. Type rand to obtain a single random number in the interval [0,1]. Typing rand again generates a different number because the MATLAB algorithm used for the rand function requires a “state” to start. MATLAB obtains this state from the computer’s CPU clock. Thus every time the rand function is used, a different result will be obtained. For example,

rand

ans =

0.6161

rand

ans =

0.5184

Type rand (n) to obtain an n x n matrix of uniformly distributed random numbers in the interval [0, 1]. Type rand (m, n) to obtain an m x n matrix of random numbers. For example, to create a 1 x 100 vector y having 100 random values in the interval [0, 1], type y = rand (1 ,100 ).Using the rand function this way is equivalent to typing rand 100 times. Even though there is a single call to the rand function, the rand function’s calculation has the effect of using a different state to obtain each of the 100 numbers so that they will be random.

Use Y = rand (m, n , p, … ) to generate a multidimensional array Y having random elements. Typing rand ( size (A) ) produces an array of random entries that is the same size as A.

For example, the following script makes a random choice between two equally probable alternatives.

if rand < 0.5

disp (‘heads’ )

else

disp (‘tails’)

end

In order to compare the results of two or more simulations, you sometimes will need to generate the same sequence of random numbers each time the simulation runs. To generate the same sequence, you must use the same state each time. The current state s of the uniform number generator can be obtained by typing s = rand (‘state’). This returns a 35-element vector containing the current state of the uniform generator. To set the state of the generator to s, type,rand (‘state’, s). Typing rand (‘state’, 0) resets the generator to its initial state. Typing rand (‘ state’ , j ), for integer j, resets the generator to

state j. Typing rand (‘state’, sum (100 *clock) ) resets the generator to a different state each time. Table 7.3-1 summarizes these functions.

The following session shows how to obtain the same sequence every time rand is called

» rand (‘state’,0)

» rand

ans =

0.9501

» rand

ans =

0.2311

» rand (‘state’,O}

» rand

ans =

0.9501

» rand

ans =

0.2311

You need not start with the initial state in order to generate the same sequence. To show this, continue the above session as follows.

» s = rand (‘state’};

»rand (‘state’,s}

» rand

ans =

0.6068

» rand (‘state’,s}

» rand

ans =

0.6068

You can use the rand function to generate random numbers in an interval other than [0, 1]. For example, to generate values in the interval [2, 10], first generate a random number/between 0 and I, multiply it by 8 (the difference between the upper and lower ~d then add the lower bound (2). The result is a value that is uniformly distributed in the interval [2, 10]. The general formula for generating a uniformly distributed random number y in the interval [a , b] is

y = (b – a) x+a

where x is a’random number uniformly’ distributed ‘in the interval [0, 1]. For example, to generate a vector y containing 1000 uniformly distributed random numbers in the interval [2, 10], you type y = 8*rand (1,1000) + 2. You can check the results with the mean, min, and max functions. You should obtain values close to 6, 2, and 10,respectively.

You can use rand to generate random results for games involving dice, for example, but you must use ‘it to create integers. An easier way is to-use the randperm (n) function, which generates a random permutation of the integers from 1 to n. For example, randperm (6) might generate the vector [3 2 6 4 1 5], or some other permutation of the numbers from 1 to 6. Note that randperm calls rand and therefore changes the state of the generator.

**simulations Using Random Numbers**

Simulations using random number generators can be used to analyze and find solutions to problems that are difficult or impossible to solve using standard mathematics. The following example illustrates this method.

**Normally Distributed Random Numbers**

In a sequence of normally distributed random numbers, the values near the mean are more likely to occur. We have noted that the outcomes of many processes can be described by the normal distribution. Although a uniformly distributed random variable has definite upper and lower bounds, a normally distributed random variable does not.

The MATLAB function randn will generate a single number that is normally distributed with a mean equal to 0 and a standard deviation equal to 1. Type randn (n) to obtain an n x n matrix of such numbers. Type randn (m,n) to obtain an In x n matrix of random numbers .

The functions for retrieving and specifying the state of the normally distributed random number generator are identical to those for the uniformly distributed generator, except that r andn ( … ) replaces rand ( … ) in the syntax. These functions are summarized in Table 7.3-1.

You can generate a sequence of normally distributed numbers having a mean µ and standard deviation a from a normally distributed sequence having a mean of 0 and a standard deviation of 1. You do this by multiplying the values by a and adding: µ to each result. Thus if x is a random number with a mean of 0 and a standard derviation of 1, use the following equation to generate a new random number y having a standard deviation of a and a mean of µ.

y = σ x + µ

For example, to generate a vector y containing 2000 random numbers normally distributed with a mean of 5 and a standard deviation of 3, you type y = 3*randn (1,2000) + 5. You can check the results with the mean and st.d functions. You should obtain values close to 5 and 3,respectively.

**Functions of Random Variables **If y and x are linearly related, as

y = bx + c

and if x is normally distributed with a mean µ x and standard deviation ax, it can be shown that the mean and standard deviation of y are given by

However, it is easy ‘to see that the means and standard deviations do not combine in a straightforward fashion when the variables are related by a nonlinear function. For example, if x is-normally distributed with a mean of 0, and if y = X², it is easy to see that the mean of y is not 0, but is positive. In addition, y is not normally distributed.

Some advanced methods are available for deriving a formula for the mean and variance of y = f(x), but for our purposes, the simplest way is to use random number simulation.

It was noted in the previous section that the mean of the sum (or difference) of two independent normally distributed random variables equals the sum (or difference) of their means, but the variance is always the sum of the two variances. However, if z is a nonlinear function of x and y, then the mean and variance of z cannot be found with a simple formula. In fact, the distribution of z will not even be normal. This outcome is illustrated by the following example.