05-Jan-2023 21:03:52 dueling_idiots_test(): MATLAB/Octave version 9.8.0.1380330 (R2020a) Update 2 Test dueling_idiots(). ash(): A champ plays a chump in a match of 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. What are the probabilities that the champ wins, ties, or loses the match? 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-0 = 6 BINOMIAL: 6-Choose-0 = 15 BINOMIAL: 6-Choose-0 = 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 turning on,, wired in series/parallel with sheets of 5 switches. Graphics saved as "bulb.png" bulb(): Calculate and plot the probability of a bulb turning on,, wired in series/parallel with sheets of 10 switches. Graphics saved as "bulb.png" casino(): Simulate many games of "Chuck-a-Luck" and calculate the average winnings. That is, you bet $1 each time and the winnings (W) are how much MORE money you have over and above the $trial_num betting money. The average winnings are, of course, W/trial_num. 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.0835 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.02754 1 0.0115933 2 0.0144893 3 0.0259012 4 0.0261693 5 0.012082 6 0.0154287 7 0.0319449 8 0.031176 9 0.0219715 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=1000 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.73973 Exact value = 2.71828 esim(): Sample n=10000 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.75028 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.00436 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.02484 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 100000 random values shows 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.5387 The average number of trigger-pulls/duel is 6.0251 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 10000 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 = 2.9726 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. n = Columns 1 through 13 1 2 3 4 5 6 7 8 9 10 11 12 13 Columns 14 through 26 14 15 16 17 18 19 20 21 22 23 24 25 26 Columns 27 through 39 27 28 29 30 31 32 33 34 35 36 37 38 39 Columns 40 through 52 40 41 42 43 44 45 46 47 48 49 50 51 52 Columns 53 through 65 53 54 55 56 57 58 59 60 61 62 63 64 65 Columns 66 through 78 66 67 68 69 70 71 72 73 74 75 76 77 78 Columns 79 through 91 79 80 81 82 83 84 85 86 87 88 89 90 91 Columns 92 through 104 92 93 94 95 96 97 98 99 100 101 102 103 104 Columns 105 through 117 105 106 107 108 109 110 111 112 113 114 115 116 117 Columns 118 through 130 118 119 120 121 122 123 124 125 126 127 128 129 130 Columns 131 through 143 131 132 133 134 135 136 137 138 139 140 141 142 143 Columns 144 through 156 144 145 146 147 148 149 150 151 152 153 154 155 156 Columns 157 through 169 157 158 159 160 161 162 163 164 165 166 167 168 169 Columns 170 through 182 170 171 172 173 174 175 176 177 178 179 180 181 182 Columns 183 through 195 183 184 185 186 187 188 189 190 191 192 193 194 195 Columns 196 through 208 196 197 198 199 200 201 202 203 204 205 206 207 208 Columns 209 through 221 209 210 211 212 213 214 215 216 217 218 219 220 221 Columns 222 through 234 222 223 224 225 226 227 228 229 230 231 232 233 234 Columns 235 through 247 235 236 237 238 239 240 241 242 243 244 245 246 247 Columns 248 through 260 248 249 250 251 252 253 254 255 256 257 258 259 260 Columns 261 through 273 261 262 263 264 265 266 267 268 269 270 271 272 273 Columns 274 through 286 274 275 276 277 278 279 280 281 282 283 284 285 286 Columns 287 through 299 287 288 289 290 291 292 293 294 295 296 297 298 299 Columns 300 through 312 300 301 302 303 304 305 306 307 308 309 310 311 312 Columns 313 through 325 313 314 315 316 317 318 319 320 321 322 323 324 325 Columns 326 through 338 326 327 328 329 330 331 332 333 334 335 336 337 338 Columns 339 through 351 339 340 341 342 343 344 345 346 347 348 349 350 351 Columns 352 through 364 352 353 354 355 356 357 358 359 360 361 362 363 364 Columns 365 through 377 365 366 367 368 369 370 371 372 373 374 375 376 377 Columns 378 through 390 378 379 380 381 382 383 384 385 386 387 388 389 390 Columns 391 through 403 391 392 393 394 395 396 397 398 399 400 401 402 403 Columns 404 through 416 404 405 406 407 408 409 410 411 412 413 414 415 416 Columns 417 through 429 417 418 419 420 421 422 423 424 425 426 427 428 429 Columns 430 through 442 430 431 432 433 434 435 436 437 438 439 440 441 442 Columns 443 through 455 443 444 445 446 447 448 449 450 451 452 453 454 455 Columns 456 through 468 456 457 458 459 460 461 462 463 464 465 466 467 468 Columns 469 through 481 469 470 471 472 473 474 475 476 477 478 479 480 481 Columns 482 through 494 482 483 484 485 486 487 488 489 490 491 492 493 494 Columns 495 through 507 495 496 497 498 499 500 501 502 503 504 505 506 507 Columns 508 through 520 508 509 510 511 512 513 514 515 516 517 518 519 520 Columns 521 through 533 521 522 523 524 525 526 527 528 529 530 531 532 533 Columns 534 through 546 534 535 536 537 538 539 540 541 542 543 544 545 546 Columns 547 through 559 547 548 549 550 551 552 553 554 555 556 557 558 559 Columns 560 through 572 560 561 562 563 564 565 566 567 568 569 570 571 572 Columns 573 through 585 573 574 575 576 577 578 579 580 581 582 583 584 585 Columns 586 through 598 586 587 588 589 590 591 592 593 594 595 596 597 598 Columns 599 through 611 599 600 601 602 603 604 605 606 607 608 609 610 611 Columns 612 through 624 612 613 614 615 616 617 618 619 620 621 622 623 624 Columns 625 through 637 625 626 627 628 629 630 631 632 633 634 635 636 637 Columns 638 through 650 638 639 640 641 642 643 644 645 646 647 648 649 650 Columns 651 through 663 651 652 653 654 655 656 657 658 659 660 661 662 663 Columns 664 through 676 664 665 666 667 668 669 670 671 672 673 674 675 676 Columns 677 through 689 677 678 679 680 681 682 683 684 685 686 687 688 689 Columns 690 through 702 690 691 692 693 694 695 696 697 698 699 700 701 702 Columns 703 through 715 703 704 705 706 707 708 709 710 711 712 713 714 715 Columns 716 through 728 716 717 718 719 720 721 722 723 724 725 726 727 728 Columns 729 through 741 729 730 731 732 733 734 735 736 737 738 739 740 741 Columns 742 through 754 742 743 744 745 746 747 748 749 750 751 752 753 754 Columns 755 through 767 755 756 757 758 759 760 761 762 763 764 765 766 767 Columns 768 through 780 768 769 770 771 772 773 774 775 776 777 778 779 780 Columns 781 through 793 781 782 783 784 785 786 787 788 789 790 791 792 793 Columns 794 through 800 794 795 796 797 798 799 800 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 10000 No-switch strategy wins 3364 times Switch strategy wins 6636 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 100000 normal random numbers illustrates how they tend to be distributed. Graphics saved as "normal.png" odd(): Simulate 1000 games of odd-person-out, for 2 players. 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...,100 The average number of flips for 2 players is 1.998 Graphics saved as "odd.png" odd(): Simulate 1000 games of odd-person-out, for 3 players. 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...,100 The average number of flips for 3 players is 1.321 Graphics saved as "odd.png" odd(): Simulate 1000 games of odd-person-out, for 4 players. 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...,100 The average number of flips for 4 players is 2.013 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 = 8.941 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.12 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.7177 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.4355 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 = 4.01 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 5000 values of the sum of 2 random variables, each variable uniform from 0 to 1, and independent. Plot a histogram of the sums using 100 bins. label = 'Histogram of the sum of 2 Random Variables' Graphics saved as "xplusy.png" xplusy(): Produce 5000 values of the sum of 6 random variables, each variable uniform from 0 to 1, and independent. Plot a histogram of the sums using 100 bins. label = 'Histogram of the sum of 6 Random Variables' 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 20000 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.