C Matlab CRAPS ME 2002 2010 Fall week
C/ Matlab –CRAPS (雙骰子 遊戲) ME 2002 -2010 Fall. week 05 • Switch 敘述 的應用
將 if 敘述改成 switch 敘述, 其程式如下: /* C program 10 */ #include <stdio. h> main() { int a; // scan –掃瞄鍵盤 scanf(“%d”, &a); //如同 input-(matlab) switch(a % 2) { case 0: a = a / 2; break; default: a = 3 * a + 1; break; } printf("a = %d. ", a); //fprintf (matlab) }
// C Program—SWITCH CASE… END #include <stdio. h> /* program 12 */ #include <stdlib. h> #include <time. h> main() { int a, b; srand((unsigned)time(NULL)); a = rand()%6 + 1; b = rand()%6 + 1; printf(“a = %d, b = %d. n”, a, b); switch( a + b ) { case 7: case 11: printf("win"); break; case 2: case 3: case 12: printf("craps"); break; default: printf("point"); } }
對於 switch 敘述,如果有兩個或多個 case 執行的 指令是相同的,那麼我們可以將這些 case 合併在一起, 如 program 12 所示。如果不把他們合併在一起,那麼 程式會顯得冗長且較難找到相關性,如下所示 : switch( a + b ) { case 2: printf("craps"); break; case 3: printf("craps"); break; case 7: printf("win"); break; case 11: printf("win"); break; case 12: printf("craps"); break; default: printf("point"); break; } }
Monte Carlo Simulation of the Craps Dice Game Sencer Koç (Note: Google Search – Today: 2007/CE 2002)
Basics • The player rolling the dice is the "shooter". Shooters first throw in a round of Craps is called the Come Out roll. If you roll a 7 or 11, you win and the round is over before it started. • If you roll a 2, 3, or 12 that's a Craps and you lose: again, it's over before it started. • Any other number becomes the Point. The purpose of the Come Out roll is to set the Point, which can be any of 4, 5, 6, 8, 9 or 10.
Objective • The basic objective in Craps is for the shooter to win by tossing the Point again before he tosses a 7. That 7 is called Out 7 to differentiate it from the 7 on the Come Out roll. • If the Point is tossed, the shooter and his fellow bettors win and the round is over. If the shooter tosses Out 7, they lose and the round is over. • If the toss is neither the Point nor Out 7, the round continues and the dice keep rolling.
Questions • What is the probability that the roller wins? – Note that this is not a simple problem. • The probability of win at later rolls depends on the point value, e. g. , if the point is 8, P(8)=5/36 ({2, 6}, {3, 5}, {4, 4}, {5, 3}, {6, 2}) and if the point is 10, P(10)=1/36 ({5, 5}). • How many rolls (on the average) will the game last?
Simulation and MATLAB • We will simulate the craps game on a computer using MATLAB. • The command rand(1) – Uniformly distributed between 0 and 1 – round(rand(1, 2)*6+0. 5)simulates a single roll of a pair of dice. • The following MATLAB code simulates M games.
MATLAB Code M = 10; % Number of simulations lost = 0; win = 0; gamelength = zeros(1, M); disp(' win loss gamelength') data = zeros(10000, 3); for k=1: M stop = 0; curr_gamelength = 1; x = sum(round(rand(1, 2)*6+0. 5)); if (x==7) | (x==11) win = win+1; stop = 1; elseif (x==2) | (x==3) | (x==12) lost = lost+1; stop = 1; else point = x; end
MATLAB Code (Continued) while ~stop curr_gamelength = curr_gamelength+1; x = sum(round(rand(1, 2)*6+0. 5)); if (x==7) lost=lost+1; stop = 1; elseif (x==point) win = win+1; stop = 1; end gamelength(k) = curr_gamelength; disp(sprintf('%8. 0 f '. . . , win, lost, curr_gamelength)) data(k, : ) = [win lost curr_gamelength]; end
Generation of Useful Data • Remember we are using a random number generator! • We also need a larger sample size. We will run the problem for many more games! • Let us now try 1000 games and divide #wins and #loss by M to determine the probabilities (comment out the disp commands to suppress output of data). • Also evaluate the mean of gamelengths. • Our point estimators are – P(win) = data(1000, 1)/1000 – P(loss) = data(1000, 2)/1000 – L = mean(data(: , 3))
- Slides: 16