Monte Carlo Simulation
Monte Carlo simulation answers a question about an uncertain system by drawing random numbers and reading each one as an event: a day's demand, a lead time, a treatment, a service time. This section solves all five examples of Exercise 5 step by step: inventory control, a dentist clinic, the inverse transform for the exponential distribution, a queue with exponential times, and a manufacturing facility.
Objectives
- Turn a frequency table into two-digit random number intervals.
- Compute the mean and standard deviation of demand and lead time, then the economic order quantity (EOQ) and the reorder level (ROL).
- Build the inventory simulation table and decide when to order.
- Simulate a queue with scheduled arrivals and find the average waiting time.
- Generate exponential random variates with the inverse transform.
- Use a mixed congruential generator to simulate a manufacturing facility.
1. Example 1: inventory control
Suppose you have the demand for the past 100 days and the lead time in which the ordered quantity arrived. We need to know the demand in the coming days, the possible lead time, and when to order.
Demand for the past 100 days:
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|
| 22 | 44 | 33 | 57 | 41 | 45 | 49 | 47 | 44 | 44 |
| 48 | 45 | 33 | 55 | 41 | 42 | 50 | 72 | 46 | 43 |
| 66 | 40 | 70 | 43 | 39 | 49 | 44 | 66 | 39 | 31 |
| 52 | 42 | 44 | 31 | 42 | 34 | 38 | 38 | 48 | 31 |
| 52 | 43 | 50 | 57 | 42 | 44 | 43 | 40 | 53 | 48 |
| 52 | 49 | 50 | 37 | 40 | 28 | 46 | 47 | 31 | 38 |
| 26 | 30 | 62 | 35 | 41 | 42 | 48 | 39 | 38 | 47 |
| 32 | 41 | 43 | 43 | 64 | 51 | 55 | 31 | 55 | 46 |
| 52 | 45 | 44 | 56 | 34 | 52 | 50 | 56 | 59 | 58 |
| 58 | 44 | 57 | 53 | 48 | 54 | 33 | 27 | 57 | 39 |
Lead time:
| Lead time | Frequency |
|---|---|
| 3 days | 28% |
| 4 days | 44% |
| 5 days | 28% |
For the lead time we may have a standard frequency distribution rather than the given frequencies, to find the random numbers that correspond to a particular lead time.
Suppose that the inventory control has these costs:
| Symbol | Cost of | Value |
|---|---|---|
| placing an order | 100 pound/order | |
| holding an item in stock | 0.1 pound/unit/day | |
| shortage | 5 pounds/unit |
When you have a shortage, you have to bring items from the local market at a higher price.
Step 1: mean and standard deviation
The reorder level and the other quantities come from statistical calculations on the available data. The 100 demand values add up to , and their squares add up to .
The mean demand and its standard deviation are:
The mean lead time and its standard deviation are:
Note on the exercise sheet. The sheet writes the mean lead time as
400/4 = 4. The division is by100:400 / 100 = 4. The answer of 4 days stands.
Step 2: random numbers for the demand
We find the histogram of the demand, calculate the frequency and the cumulative frequency, and assign two-digit random numbers (from 00 to 99) to each interval according to its frequency. An interval with frequency f takes f numbers, and it ends at its cumulative frequency minus 1. When we predict the demand, we take the average value of the demand in each interval.
| Demand interval | Average demand | Frequency | Cumulative frequency | Random numbers |
|---|---|---|---|---|
| 20 - 24 | 22 | 1 | 1 | 00 |
| 25 - 29 | 27 | 3 | 4 | 01 - 03 |
| 30 - 34 | 32 | 12 | 16 | 04 - 15 |
| 35 - 39 | 37 | 10 | 26 | 16 - 25 |
| 40 - 44 | 42 | 26 | 52 | 26 - 51 |
| 45 - 49 | 47 | 17 | 69 | 52 - 68 |
| 50 - 54 | 52 | 13 | 82 | 69 - 81 |
| 55 - 59 | 57 | 12 | 94 | 82 - 93 |
| 60 - 64 | 62 | 2 | 96 | 94 - 95 |
| 65 - 69 | 67 | 2 | 98 | 96 - 97 |
| 70 - 74 | 72 | 2 | 100 | 98 - 99 |
Note on the exercise sheet. The sheet says the demand ranges from 20 to 70. The data runs from 22 to 72. The table is correct: its eleven intervals, from
20 - 24to70 - 74, hold all 100 values, and counting the values again gives the same frequencies.
Step 3: random numbers for the lead time
The same calculation applies to the lead time:
| Lead time | Frequency | Cumulative frequency | Random numbers |
|---|---|---|---|
| 3 | 28 | 28 | 00 - 27 |
| 4 | 44 | 72 | 28 - 71 |
| 5 | 28 | 100 | 72 - 99 |
Step 4: the economic order quantity
Whenever the stock falls below a certain limit, the reorder level, we order this same quantity of 300 units, because this is a fixed quantity system.
Step 5: the reorder level
K is a constant that sets the probability of a stock shortage. For K = 1.65 the chance of a shortage is 5%, so we are 95% sure that there will not be any shortage. For K = 2.33 there is only a 1% chance of a shortage.
For K = 1.65:
Note on the exercise sheet. The sheet writes
ROL = 245. The exact value is about 245.6, so 245 is rounded down. We keep 245, because the simulation table uses it. With 246 the orders fall on the same days, since the lowest quantity that gives no order is 248.
Step 6: starting the simulation
We start the simulation with EOQ = 300 and ROL = 245 units. We assume the initial stock is 300 units on Friday evening, so the simulation runs from Monday onwards.
The random numbers from 00 to 99 can be generated in Excel or by using other random number generators.
Note on the exercise sheet. The sheet says to put
100into therand()function. In Excel,RAND()takes no argument, and it gives a number below 1. Multiply by 100 instead:INT(RAND()*100)gives a number from00to99.
The sheet takes its random numbers from a printed table of two-digit numbers:
- For demand, from the left of the first row:
81, 78, 89, 59, 40, 33, 60, 67, 19, 21. - For lead time, from the right of the last row:
65, 67, 22. We draw one only when we place an order.
Note on the exercise sheet. The text names the demand numbers as
81, 78, 59, which skips one. The first row and the simulation table both have81, 78, 89, 59. Wednesday uses89, so its demand is 57.
Step 7: the simulation table
The table covers two working weeks (10 working days), with initial stock = 300 units, EOQ = 300 units and ROL = 245 units:
| Day | Initial stock | R. No. for demand | Daily demand | Shortage | Quantity received | Final stock | Quantity in hand + order | Quantity ordered | R. No. for lead time | Lead time |
|---|---|---|---|---|---|---|---|---|---|---|
| Mon | 300 | 81 | 52 | - | - | 248 | 248 | 0 | - | - |
| Tue | 248 | 78 | 52 | - | - | 196 | 196 | 300 | 65 | 4 |
| Wed | 196 | 89 | 57 | - | - | 139 | 439 | 0 | - | - |
| Thu | 139 | 59 | 47 | - | - | 92 | 392 | 0 | - | - |
| Fri | 92 | 40 | 42 | - | - | 50 | 350 | 0 | - | - |
| Mon | 50 | 33 | 42 | - | 300 | 308 | 308 | 0 | - | - |
| Tue | 308 | 60 | 47 | - | - | 261 | 261 | 0 | - | - |
| Wed | 261 | 67 | 47 | - | - | 214 | 214 | 300 | 67 | 4 |
| Thu | 214 | 19 | 37 | - | - | 177 | 477 | 0 | - | - |
| Fri | 177 | 21 | 37 | - | - | 140 | 440 | 0 | - | - |
The rules behind each row:
- The initial stock is the previous final stock.
- The demand comes from the random number through the table in Step 2:
81falls in69 - 81, so the demand is 52. - Final stock = initial stock - daily demand + quantity received.
- Quantity in hand + order = final stock + the order on the way.
- If that quantity falls below
ROL, we orderEOQand draw a lead time from the table in Step 3.
Now follow the first days:
- First Monday:
300 - 52 = 248units. This is above 245, so there is no order. - Tuesday: the initial stock is 248 and the final stock is
248 - 52 = 196. This is below 245, so we have to reorder. The random number65falls in28 - 71, so the lead time is 4 days. - Wednesday to Friday: the final stock is below 245, but the 300 units on the way count:
139 + 300 = 439,92 + 300 = 392and50 + 300 = 350. There is no new order. - Second Wednesday: the stock has fallen to 214 with nothing on the way, so we order again. The random number
67gives a lead time of 4 days.
Note on the exercise sheet. The sheet defines quantity in hand + order as final stock + quantity ordered. On the day of an order that gives
196 + 300 = 496and214 + 300 = 514, but the table shows 196 and 214. In the table, an order joins this column from the next day. We keep the table, and the order days are the same either way.
Step 8: when the orders arrive, and the costs
When the lead time is 4 working days from Tuesday, the quantity ordered is received on next Monday, and the final stock is:
The second ordered quantity is received on next Tuesday, after the 10 days of the table.
We can also calculate parameters like the cost in a certain period:
- Cost of holding inventory = holding cost per unit × final stock.
- Cost of ordering = number of orders × order cost.
2. Example 2: a dentist clinic
At a dentist clinic, apply Monte Carlo simulation to find the average waiting time of customers. The simulation starts at 8 a.m., and the arrival of customers is scheduled (in other cases it may be random, depending on some probability distribution). The first customer arrives at 8 a.m., and one customer comes every 30 minutes for one of five different treatments. The five treatment services are filling, crown, cleaning, extraction and checkup, with different probabilities and service times.
Step 1: the random number intervals
We do not know the treatment service a customer is coming for. That guess comes from random numbers generated by any generator, through this table:
| Service | Service time | Probability | Cumulative probability | Random number interval |
|---|---|---|---|---|
| Filling | 40 min | 0.40 | 0.40 | 00 - 39 |
| Crown | 60 min | 0.15 | 0.55 | 40 - 54 |
| Cleaning | 15 min | 0.15 | 0.70 | 55 - 69 |
| Extraction | 45 min | 0.10 | 0.80 | 70 - 79 |
| Checkup | 15 min | 0.20 | 1.00 | 80 - 99 |
Step 2: which service?
The random numbers 40, 82, 11, 34, 25, 66, 17, 79 predict the service for 8 patients:
| Patient | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Arrival time | 8:00 | 8:30 | 9:00 | 9:30 | 10:00 | 10:30 | 11:00 | 11:30 |
| Random number of service | 40 | 82 | 11 | 34 | 25 | 66 | 17 | 79 |
| Category of service | Crown | Checkup | Filling | Filling | Filling | Cleaning | Filling | Extraction |
| Service time (min) | 60 | 15 | 40 | 40 | 40 | 15 | 40 | 45 |
Note that 40 is a crown, not a filling: the filling interval ends at 39.
Step 3: the simulation table
A service starts at the later of the arrival and the previous service end. The waiting time is the service start minus the arrival.
| Patient | Arrival time | Service time (min) | Service starts at | Service ends at | Waiting time (min) |
|---|---|---|---|---|---|
| 1 | 8:00 | 60 | 8:00 | 9:00 | 0 |
| 2 | 8:30 | 15 | 9:00 | 9:15 | 30 |
| 3 | 9:00 | 40 | 9:15 | 9:55 | 15 |
| 4 | 9:30 | 40 | 9:55 | 10:35 | 25 |
| 5 | 10:00 | 40 | 10:35 | 11:15 | 35 |
| 6 | 10:30 | 15 | 11:15 | 11:30 | 45 |
| 7 | 11:00 | 40 | 11:30 | 12:10 | 30 |
| 8 | 11:30 | 45 | 12:10 | 12:55 | 40 |
| Total | 220 |
For example, patient 2 arrives at 8:30 while the dentist is busy until 9:00, so the wait is 30 minutes. That service ends at 9:00 + 15 = 9:15, so patient 3, who arrives at 9:00, waits 15 minutes.
The average waiting time is:
Note on the exercise sheet. The sheet asks for the average waiting time but does not state it. Its table also runs off the page: it prints 6 of the 8 waits and 7 of the 8 service ends. The last values (the end 12:55 and the waits 30 and 40) and the average of 27.5 minutes come from the same rule.
3. Example 3: the inverse transform for the exponential distribution
Let have the exponential distribution with mean . The distribution function is:
To find , we set and solve for :
To generate the desired random variate, we first generate and then let:
It is possible in this case to use instead of , since and have the same distribution. This saves a subtraction.
4. Example 4: a queue with exponential times
Suppose that in a queueing system the inter-arrival time is exponentially distributed with mean , and the service time is also exponentially distributed with mean . From the simulation table for the arrival, service and departure times of 8 customers, calculate the average waiting time for a customer.
Step 1: the random variates
The inter-arrival time and the service time come from the inverse transform equations:
Here and lie between 0 and 1. Given a stream of two-digit random numbers from 00 to 99, we get by dividing each number by 100. The streams are:
u1 = 20, 23, 86, 09, 92, 35, 38, 01u2 = 21, 44, 27, 70, 73, 36, 59, 42
For the first customer:
Step 2: the simulation table
Each variate is rounded to two decimals and then added, as the sheet does. The arrival time is the previous arrival plus . A service starts at the later of the arrival and the previous departure, and the customer departs after . The waiting time is the service start minus the arrival.
| Customer | Inter-arrival time | Arrival time | Service time | Departure time | Waiting time | ||
|---|---|---|---|---|---|---|---|
| 1 | 20 | 3.22 | 3.22 | 21 | 2.08 | 5.30 | 0 |
| 2 | 23 | 2.94 | 6.16 | 44 | 1.09 | 7.25 | 0 |
| 3 | 86 | 0.30 | 6.46 | 27 | 1.74 | 8.99 | 0.79 |
| 4 | 09 | 4.82 | 11.28 | 70 | 0.47 | 11.75 | 0 |
| 5 | 92 | 0.17 | 11.45 | 73 | 0.42 | 12.17 | 0.30 |
| 6 | 35 | 2.10 | 13.55 | 36 | 1.36 | 14.91 | 0 |
| 7 | 38 | 1.94 | 15.49 | 59 | 0.70 | 16.19 | 0 |
| 8 | 01 | 9.21 | 24.70 | 42 | 1.15 | 25.85 | 0 |
Only two customers wait:
- Customer 3 arrives at 6.46, and customer 2 leaves at 7.25: the wait is
7.25 - 6.46 = 0.79, and the departure is7.25 + 1.74 = 8.99. - Customer 5 arrives at 11.45, and customer 4 leaves at 11.75: the wait is
11.75 - 11.45 = 0.30, and the departure is11.75 + 0.42 = 12.17.
The average waiting time is:
We can calculate other measures of performance from the same table.
Note on the exercise sheet. For
u2 = 27, 70, 42the sheet has service times1.75, 0.48, 1.16. With the stated they are1.74, 0.47, 1.15, and the table above uses these. The sheet's whole column matches instead. So the sheet's departures of9.00, 11.76, 12.18, 25.86become8.99, 11.75, 12.17, 25.85, and its second wait of0.31becomes0.30. The sheet's average,(0.79 + 0.31) / 8 = 0.14, rounds to the same value.
5. Example 5: manufacturing
Given the units produced in a manufacturing facility and the number of vehicles available for transportation at a particular station, with these probabilities:
| Units produced | Random number interval | Vehicles available | Random number interval | ||
|---|---|---|---|---|---|
| 500 | 0.06 | 00 - 05 | 5 | 0.16 | 00 - 15 |
| 550 | 0.14 | 06 - 19 | 6 | 0.36 | 16 - 51 |
| 600 | 0.20 | 20 - 39 | 7 | 0.20 | 52 - 71 |
| 650 | 0.40 | 40 - 79 | 8 | 0.16 | 72 - 87 |
| 700 | 0.20 | 80 - 99 | 9 | 0.12 | 88 - 99 |
Step 1: the mixed congruential generator
A mixed congruential generator with the three parameters a, c and m generates two-digit random numbers between 00 and 99:
- For :
a = 21,c = 3,m = 100, seed . - For :
a = 41,c = 7,m = 100, seed .
Note on the exercise sheet. The sheet writes , without the outer brackets. Its steps compute the bracketed form, for example
423 mod 100 = 23, and every value of both streams follows that form.
For :
For :
And so on to :
| Stream | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| 20 | 23 | 86 | 09 | 92 | 35 | 38 | 01 | 24 | 07 | |
| 01 | 48 | 75 | 82 | 69 | 36 | 83 | 10 | 17 | 04 |
Step 2: the simulation table for 10 days
The ten days use to , so day 1 uses the seed itself. Each number is read through the interval table above.
| Day | R. No. for units produced | Units produced | R. No. for vehicles availability | Vehicles available |
|---|---|---|---|---|
| 1 | 20 | 600 | 01 | 5 |
| 2 | 23 | 600 | 48 | 6 |
| 3 | 86 | 700 | 75 | 8 |
| 4 | 09 | 550 | 82 | 8 |
| 5 | 92 | 700 | 69 | 7 |
| 6 | 35 | 600 | 36 | 6 |
| 7 | 38 | 600 | 83 | 8 |
| 8 | 01 | 500 | 10 | 5 |
| 9 | 24 | 600 | 17 | 6 |
| 10 | 07 | 550 | 04 | 5 |
| Total | 6000 | 64 |
Average number of units produced per day:
Average number of available vehicles per day:
6. Common mistakes
- Using the limits of an interval as the demand. The demand drawn from an interval is its average:
81falls in69 - 81, and that row gives 52. - Missing a boundary.
40is a crown because the filling interval ends at39, and79is an extraction because its interval is70 - 79. - Testing the final stock alone against ROL. On the first Wednesday the final stock is 139, but the 300 units on the way give 439, so there is no new order.
- Counting lead time in calendar days. The count is in working days: 4 working days from Tuesday is next Monday.
- Starting every service on arrival. A service starts at the later of the arrival and the previous end. Waiting time is measured from the arrival.
- Forgetting to divide by 100, or dropping the minus sign. and . Since is negative, the minus sign makes positive.
7. Practice
Solve each problem, then check the answers at the end of this page.
- With the demand and lead time tables of Example 1, find the demand for the random numbers
94, 51, 37and the lead time for20, 71, 72. - Use the cost formulas of Example 1 to find the cost of holding inventory and the cost of ordering for the 10 days of the simulation table.
- At the dentist clinic of Example 2, 4 patients arrive every 30 minutes from 8:00, with the random numbers
39, 55, 80, 70. Find the waiting times and their average. - With and as in Example 4, simulate 3 customers with
u1 = 50, 90, 40andu2 = 30, 60, 80. Find the average waiting time. - Use the generator of Example 5 with the seed to get 4 random numbers (day 1 uses the seed), then find the units produced on each day and their average.
Key takeaways
- An interval takes as many two-digit numbers as its frequency, and it ends at its cumulative frequency minus 1.
- EOQ sets how much to order, and ROL sets when.
- An order counts from the day after it is placed, and it enters the stock on the day it arrives.
- In a queue, the waiting time is the service start minus the arrival.
- The inverse transform gives the exponential variate .
- A mixed congruential generator computes .
Answers
Practice 1
94 falls in 94 - 95, so the demand is 62. 51 falls in 26 - 51, so the demand is 42. 37 also falls in 26 - 51, so the demand is 42. For the lead time, 20 falls in 00 - 27 and gives 3 days, 71 falls in 28 - 71 and gives 4 days, and 72 falls in 72 - 99 and gives 5 days.
Practice 2
The final stocks add up to 248 + 196 + 139 + 92 + 50 + 308 + 261 + 214 + 177 + 140 = 1825 unit-days. The cost of holding inventory is 0.1 × 1825 = 182.5 pounds. The table places 2 orders, so the cost of ordering is 2 × 100 = 200 pounds. There is no shortage, so there is no shortage cost.
Practice 3
39 is a filling (40 min), 55 is a cleaning (15 min), 80 is a checkup (15 min) and 70 is an extraction (45 min).
| Patient | Arrival time | Service time (min) | Service starts at | Service ends at | Waiting time (min) |
|---|---|---|---|---|---|
| 1 | 8:00 | 40 | 8:00 | 8:40 | 0 |
| 2 | 8:30 | 15 | 8:40 | 8:55 | 10 |
| 3 | 9:00 | 15 | 9:00 | 9:15 | 0 |
| 4 | 9:30 | 45 | 9:30 | 10:15 | 0 |
The average waiting time is 10 / 4 = 2.5 minutes.
Practice 4
| Customer | Arrival time | Departure time | Waiting time | ||||
|---|---|---|---|---|---|---|---|
| 1 | 50 | 1.39 | 1.39 | 30 | 1.60 | 2.99 | 0 |
| 2 | 90 | 0.21 | 1.60 | 60 | 0.68 | 3.67 | 1.39 |
| 3 | 40 | 1.83 | 3.43 | 80 | 0.30 | 3.97 | 0.24 |
The average waiting time is (1.39 + 0.24) / 3 = 0.54.
Practice 5
The numbers 05, 08, 71, 94 give 500, 550, 650 and 700 units. The total is 2400, so the average is 2400 / 4 = 600 units/day.