Home License -- for personal use only. Not for government, academic, research, commercial, or other organizational use. 13-May-2025 13:23:05 dueling_idiots_test(): MATLAB/Octave version 9.11.0.2358333 (R2021b) Update 7 Test dueling_idiots(). ash(): A champ plays a chump at N games of chess. P is the probability an individual game is won by the champ. Q is the probability an individual game ends in a tie. For this code, P = Q = 1/3. total = 0.4756 Probability of a tie is 0.0487686 Probability the champ wins is 0.475616 Probability the challenger wins is 0.475616 balls(): An urn contains N numbered balls. A ball is drawn repeatedly from the urn, examined, and replaced. This file calculates: (1) E(n), the average number of drawings (with replacement) of numbered balls before a repetition occurs; (2) T, the largest number of drawings that still allows the probability there is NOT a repetition is still greater than 1/2. E = 3.66022 T = 4 balls(): An urn contains N numbered balls. A ball is drawn repeatedly from the urn, examined, and replaced. This file calculates: (1) E(n), the average number of drawings (with replacement) of numbered balls before a repetition occurs; (2) T, the largest number of drawings that still allows the probability there is NOT a repetition is still greater than 1/2. E = 12.21 T = 12 balls(): An urn contains N numbered balls. A ball is drawn repeatedly from the urn, examined, and replaced. This file calculates: (1) E(n), the average number of drawings (with replacement) of numbered balls before a repetition occurs; (2) T, the largest number of drawings that still allows the probability there is NOT a repetition is still greater than 1/2. E = 39.3032 T = 37 balls(): An urn contains N numbered balls. A ball is drawn repeatedly from the urn, examined, and replaced. This file calculates: (1) E(n), the average number of drawings (with replacement) of numbered balls before a repetition occurs; (2) T, the largest number of drawings that still allows the probability there is NOT a repetition is still greater than 1/2. E = 124.999 T = 118 balls(): An urn contains N numbered balls. A ball is drawn repeatedly from the urn, examined, and replaced. This file calculates: (1) E(n), the average number of drawings (with replacement) of numbered balls before a repetition occurs; (2) T, the largest number of drawings that still allows the probability there is NOT a repetition is still greater than 1/2. E = 396 T = 372 baseball() Calculate and plot (as a function of p) the ratio of the probabilities of a team winning at least 81p games out of 81 games to the probability of winning at least 162p games out of 162 games, where p is the probability of winning any individual game. Present results graphically. Graphics saved as "baseball.png" biased(): A game of odd man out is played, in which N people each flip a coin. If one person's coin does not match all the others, that person is the odd man out and the game is over. In this version, N-1 people have fair coins, and one has a biased coin whose probability of heads is Q. Estimate the average number of games played. Average number of games = 1.356 Theoretical number = 1.33333 biased(): A game of odd man out is played, in which N people each flip a coin. If one person's coin does not match all the others, that person is the odd man out and the game is over. In this version, N-1 people have fair coins, and one has a biased coin whose probability of heads is Q. Estimate the average number of games played. Average number of games = 1.344 Theoretical number = 1.33333 biased(): A game of odd man out is played, in which N people each flip a coin. If one person's coin does not match all the others, that person is the odd man out and the game is over. In this version, N-1 people have fair coins, and one has a biased coin whose probability of heads is Q. Estimate the average number of games played. Average number of games = 1.371 Theoretical number = 1.33333 biased(): A game of odd man out is played, in which N people each flip a coin. If one person's coin does not match all the others, that person is the odd man out and the game is over. In this version, N-1 people have fair coins, and one has a biased coin whose probability of heads is Q. Estimate the average number of games played. Average number of games = 2.047 Theoretical number = 2 biased(): A game of odd man out is played, in which N people each flip a coin. If one person's coin does not match all the others, that person is the odd man out and the game is over. In this version, N-1 people have fair coins, and one has a biased coin whose probability of heads is Q. Estimate the average number of games played. Average number of games = 2.032 Theoretical number = 2 biased(): A game of odd man out is played, in which N people each flip a coin. If one person's coin does not match all the others, that person is the odd man out and the game is over. In this version, N-1 people have fair coins, and one has a biased coin whose probability of heads is Q. Estimate the average number of games played. Average number of games = 1.996 Theoretical number = 2 biased(): A game of odd man out is played, in which N people each flip a coin. If one person's coin does not match all the others, that person is the odd man out and the game is over. In this version, N-1 people have fair coins, and one has a biased coin whose probability of heads is Q. Estimate the average number of games played. Average number of games = 3.161 Theoretical number = 3.2 biased(): A game of odd man out is played, in which N people each flip a coin. If one person's coin does not match all the others, that person is the odd man out and the game is over. In this version, N-1 people have fair coins, and one has a biased coin whose probability of heads is Q. Estimate the average number of games played. Average number of games = 3.295 Theoretical number = 3.2 biased(): A game of odd man out is played, in which N people each flip a coin. If one person's coin does not match all the others, that person is the odd man out and the game is over. In this version, N-1 people have fair coins, and one has a biased coin whose probability of heads is Q. Estimate the average number of games played. Average number of games = 3.202 Theoretical number = 3.2 biased(): A game of odd man out is played, in which N people each flip a coin. If one person's coin does not match all the others, that person is the odd man out and the game is over. In this version, N-1 people have fair coins, and one has a biased coin whose probability of heads is Q. Estimate the average number of games played. Average number of games = 5.262 Theoretical number = 5.33333 biased(): A game of odd man out is played, in which N people each flip a coin. If one person's coin does not match all the others, that person is the odd man out and the game is over. In this version, N-1 people have fair coins, and one has a biased coin whose probability of heads is Q. Estimate the average number of games played. Average number of games = 5.426 Theoretical number = 5.33333 biased(): A game of odd man out is played, in which N people each flip a coin. If one person's coin does not match all the others, that person is the odd man out and the game is over. In this version, N-1 people have fair coins, and one has a biased coin whose probability of heads is Q. Estimate the average number of games played. Average number of games = 5.583 Theoretical number = 5.33333 BINOMIAL: 6-Choose-0 = 1 BINOMIAL: 6-Choose-1 = 6 BINOMIAL: 6-Choose-2 = 15 BINOMIAL: 6-Choose-3 = 20 brownian() Simulate Brownian motion, a random walk by a particle suspended in a fluid and being hit by molecules. At successive increments of time, the particle performs independent motions of random length, uniform from -1 to 1, in arbitrary units, in both the x and the y directions. The walk duration, that is, the total number of steps, where a step is a delta-x and a delta-y pair, is defined by the user. A walk always starts at the origin. Display results graphically. Graphics saved as "brownian.png" bulb(): Calculate and plot the probability of a bulb, wired in series/parallel with sheets of switches, glowing. Graphics saved as "bulb.png" bulb(): Calculate and plot the probability of a bulb, wired in series/parallel with sheets of switches, glowing. Graphics saved as "bulb.png" casino(): Simulate 10,000 games of "Chuck-a-Luck" and calculate the average winnings. That is, you bet $1 each and the winnings (W) are how much MORE money you have over and above the $10,000 betting money. The average winnings are, of course, W/10,000. Three dice are rolled, and you can bet on any number between 1 and 6. If one die comes up with your number, you win as much as you bet. If two or three dice come up with your number, you win twice or three times your bet. Average winnings per game is -0.08105 dollars cc(): Generate a random vector, X, of length 20,009 and then calculates the correlation coefficient X(n) and X(n+j) for j = 0,1,2,...,9. J Cor(X(i),X(i+j)) 0 1.00162 1 -0.00318387 2 0.0161563 3 -0.00238527 4 0.00624957 5 0.00146516 6 0.00566084 7 -0.00354581 8 -0.00675939 9 -0.00404607 chess(): Compute the probabilities, in an N-game chess match, of the match ending in a tie, in a win for the champ, or in a win for the challenger. The probability the champ wins an individual game is p, and the probability an individual game ends in a tie is q. Present the results graphically. Graphics saved as "chess.png" chess(): Compute the probabilities, in an N-game chess match, of the match ending in a tie, in a win for the champ, or in a win for the challenger. The probability the champ wins an individual game is p, and the probability an individual game ends in a tie is q. Present the results graphically. Graphics saved as "chess.png" correlation(): Plotting X(I) versus X(I+J) can exhibit patterns if there is correlation in the data. If not, the scatter plot will be patternless. Graphics saved as "correlation.png" cpm(): Execute the Critical Path Method for a particular project. Total time required for the project = 6 early start task time slack 2 0 0 3 0 1 4 2 0 5 2 1 6 3 0 7 5 0 esim(): Sample N points in the unit interval. Put them into N bins of equal width. Let Z be the number of empty bins. N/Z is an estimate of E. Estimate for E = 2.7248 Exact value = 2.71828 esim(): Sample N points in the unit interval. Put them into N bins of equal width. Let Z be the number of empty bins. N/Z is an estimate of E. Estimate for E = 2.7533 Exact value = 2.71828 flycircle() Two flies land at random spots in the unit circle. What is the probability they are more than 1 unit apart? Probability flies are more than 1 unit apart = 0.00443 flysquare(): Two flies land at random spots in the unit square. What is the probability they are more than 1 unit apart? Probability flies are more than 1 unit apart = 0.02493 gas(): Simulate the diffusion of gas molecules in a sealed container by using the Ehrenfest ball exchange rules. The simulation starts with n molecules (i.e., balls) of one type (i.e., black) on one side of the container, and n more molecules of another type (i.e., white balls) on the other side. The two urns play the roles of the two sides of the container. To simulate the ball (molecule) movements, the program selects two random numbers from 0 to 1, which are then compared to the current probabilities of selecting a black ball from urn I and a white ball from urn II. If BOTH random numbers are greater than these two probabilities then a white ball has been selected from urn I and a black ball has been selected from urn II, and so the number black balls in urn I is increased by one while the number of white balls in urn II is increased by one. If BOTH random numbers are less than or equal to these two probabilities then a black ball has been selected from urn I and a white ball has been selected from urn II and so the number of black balls in urn I is decreased by one while the number of white balls in urn II is decreased by one. If one of the random numbers is greater than its corresponding probability while the other random number is less than its corresponding probability, then no action is taken because then a white (black) ball moves from urn I to urn II at the same time a white (black) ball moves in the opposite direction. That is, there is no net change. Then, the ball selection probabilities are recalculated and another ball exchange is simulated. Graphics saved as "gas.png" generator(): A histogram of 100,000 random values will show whether they are uniformly distributed or not. Graphics saved as "generator.png" idiots1(): A and B fight a duel, but they only have one gun, a six-shot revolver. A will spin the cylinder and fire at B. If the gun does not fire, then B will spin the cylinder and fire at A. They continue to alternate until one of them is shot. What is the probablity that A will win? What is the average number of trigger pulls? The probability A wins is 0.5469 The average number of trigger-pulls/duel is 5.9981 Graphics saved as "idiots1.png" idiots2(): Simulate a Russian-roulette duel between A and B. A fires at B one time. If A misses, B fires at A two times. If B misses, A fires at B three times, and so on. Each shot is preceded by spinning the revolver cylinder, so there is a 1/6 chance that the revolver will fire. Calculate the probability that A survives. Probability that A survives = 0.523919 kids(): Simulate 10,000 families that have children until a child is born that is the same sex as the first child, where p is the probability of a boy. Average number of children in each family = 3.0103 markov() We have four states, 1, 2, 3, 4. If we are in state I, then on the next step we will move to state J with a proability P(I,J). Simulate this process over many steps. Display the results graphically. Graphics saved as "markov.png" match() Compute the probability that N people, flipping fair coins n times, will each get the same number of heads. Match probability is 0.176197 match() Compute the probability that N people, flipping fair coins n times, will each get the same number of heads. Match probability is 0.0355442 match() Compute the probability that N people, flipping fair coins n times, will each get the same number of heads. Match probability is 0.00759002 match() Compute the probability that N people, flipping fair coins n times, will each get the same number of heads. Match probability is 0.0795892 monty(): There are three doors. A prize is behind one of the doors. You name a door. The host then opens one of the other doors, which is guaranteed not to conceal a prize. You may now choose to switch your choice to the remaining door, or not. What is the better strategy? Number of trials is 10,000 No-switch strategy wins 3298 times Switch strategy wins 6702 times needle(): A circular table top has radius r. A needle of length 2*a <= 2*r is dropped onto the table. * how many times does one end stick out over the edge? * how manu times do both ends stick out? * how many times does neither end stick out? Report the results graphically. Graphics saved as "needle1.png" Graphics saved as "needle2.png" Graphics saved as "needle3.png" normal(): Displaying a histogram of 100,000 normal random numbers illustrates how they tend to be distributed. Graphics saved as "normal.png" odd(): Simulate 1,000 games of odd-person-out, for N people. Each person flips a fair coin. The elements of the row vector duration are the number of games of length i, i.e., duration(i)=# of games that require i flips to complete, where i=1,2,3... The average number of flips for 2 players is 2.043 Graphics saved as "odd.png" odd(): Simulate 1,000 games of odd-person-out, for N people. Each person flips a fair coin. The elements of the row vector duration are the number of games of length i, i.e., duration(i)=# of games that require i flips to complete, where i=1,2,3... The average number of flips for 3 players is 1.3 Graphics saved as "odd.png" odd(): Simulate 1,000 games of odd-person-out, for N people. Each person flips a fair coin. The elements of the row vector duration are the number of games of length i, i.e., duration(i)=# of games that require i flips to complete, where i=1,2,3... The average number of flips for 4 players is 2.075 Graphics saved as "odd.png" onedwalk(): Simulate the one-dimensional, symmetrical walk with an absorbing barrier at the right. The map and connections: 1 <--> 3 <--> 4 --> 2 We start at location 1, and will stop at location 2. How long is the average walk? Average walk duration = 9.0289 Graphics saved as "onedwalk.png" onewaytodoit(): This program generates 10,000 normal random values using the Box-Muller transformation on uniform rnadom numbers. Display results as a histogram. Graphics saved as "onewaytodoit.png" onion(): Uniformly slice the unit interval, from left to right, ten times, and form the sum of the widths of the slices. Do this 5000 times and plot the distribution function of the sum. Graphics saved as "onion.png" paradox(): A simple system has an average lifetime of A. For reliability, a complex system is constructed of three simple systems. The complex system will only fail if at least two of the simple systems fail. What is the average lifetime of the complex system? Present the results graphically. Graphics saved as "paradox.png" paths(): Estimate the distribution and density functions of the random variable L denoting random path lengths across a unit square. Report results graphically. Graphics saved as "paths.png" pert(): Execute the Critical Path Method algorithm modified to incorporate random task completion times for Fig.21.5 in the text. Graphics saved as "pert1.png" Graphics saved as "pert2.png" pisim(): Sample N points (x,y) in a square. The number of points M with x^2+y^2 <= 1 lie inside the quarter circle. M/N is an estimate of pi/4. Estimate for PI is 3.1228 radar(): As the number of observations N increases, compute * PD, the probability of detection. * PFA, the probability of false alarm. Present results graphically. Graphics saved as "radar.png" randomsum(): Add a sequence of random values until you exceed 1. Let N be the number of terms in this sequence. Repeating this process M times, let D(N) be the number of times that N terms were required. Then E can be approximated by 1*D(1) + 2*D(2) + ... * I * D(I) + ... Estimate for E = 2.72033 Exact value = 2.71828 spider(): A web consists of locations 1 through 9. The web is connected, and each location is connected to some of the others. A spider begins at location 1, and a fly is trapped at location 2. By choosing random moves on the web, how long does it take the spider to find the fly? Average number of steps to reach the fly = 15.5157 Graphics saved as "spider.png" stirling1(): Compare the logarithms of n! and Stirling's approximation Present the results graphically. Graphics saved as "stirling1.png" stirling2(): Stirling approximates n! by sqrt(2*pi*n) * n^n * exp(-n). Display a plot of absolute and relative errors. Graphics saved as "stirling2.png" stirling3(): Compare n! and Stirling's approximation. Present the results graphically. Graphics saved as "stirling3.png" theory(): A line is drawn through the unit square by choosing a point x in [0,1] and then choosing a random angle between 0 and 180 degrees. This m-file plots the theoretical distribution of the length of the line between x and the intersection with the square's boundary. Graphics saved as "theory.png" thief() A thief is in a room with three doors. He chooses one at random. One door leads immediately to freedom. Another leads back to the room, after S hours. The third leads back to the room, after L hours. If the thief is returned to the room, he randomly tries a door again. How long is the average imprisonment? Estimated average total time in prison = 3.901 Exact value is 4 tub(): Compute the optimal allocation of search boats looking for the UNSINKABLE TUB. The tub is sinking either 10 miles south (probability P1) or 10 miles north of the station (probability P2 = 1 - P1). N search boats are available, and each has a PS probability of locating the tub. How can we maximize the chance of locating the tub? Report results graphically. Graphics saved as "tub.png" tub(): Compute the optimal allocation of search boats looking for the UNSINKABLE TUB. The tub is sinking either 10 miles south (probability P1) or 10 miles north of the station (probability P2 = 1 - P1). N search boats are available, and each has a PS probability of locating the tub. How can we maximize the chance of locating the tub? Report results graphically. Graphics saved as "tub.png" tub(): Compute the optimal allocation of search boats looking for the UNSINKABLE TUB. The tub is sinking either 10 miles south (probability P1) or 10 miles north of the station (probability P2 = 1 - P1). N search boats are available, and each has a PS probability of locating the tub. How can we maximize the chance of locating the tub? Report results graphically. Graphics saved as "tub.png" underdog1(): The World Series of Baseball involves up to 7 games between two teams. Suppose the stronger team has probability P of winning any one game. What is the probability that the weaker team will actually win the series? Report the results graphically. Graphics saved as "underdog1.png" underdog2(): The World Series of Baseball involves up to 7 games between two teams. Suppose the stronger team has probability P of winning any one game. What is the average duration of the World Series? Report the results graphically. Graphics saved as "underdog2.png" xplusy(): Produce 5,000 values of the sum of N random variables, each variable uniform from 0 to 1, and independent. Plot a histogram of the sums using 100 bins. Graphics saved as "xplusy.png" xplusy(): Produce 5,000 values of the sum of N random variables, each variable uniform from 0 to 1, and independent. Plot a histogram of the sums using 100 bins. Graphics saved as "xplusy.png" xpowery(): Plot the probability density function of X^Y, where X and Y are independent, and both uniform from 0 to 1. Graphics saved as "xpowery.png" xyhisto(): Produce 20,000 values of X raised to the Y power, where X and Y are both uniform from 0 to 1, and independent. Then plot a histogram of the values using 100 bins. Graphics saved as "xyhisto.png" z(): Simulate the random variable Z=X/(X-Y), where X and Y are independent and uniform from 0 to 1. The output is a histogram of Z. Graphics saved as "z.png" dueling_idiots_test(): Normal end of execution.