예전에도 몇번은 올라왔던 글 같은데
이전에 팁게에 올라온 프로그래밍 코드에 부족한 부분이 꽤 보여서 새로 짜서 결과 정리해서 올립니다.
어제 프로젝트도 끝났겠다. 심심해서 만들어봤습니다.

시뮬레이션은 100번씩 돌렸구요, 평균과 분산 모두 계산하였습니다.
혹시나 모르시는 분들을 위해 분산의 의미는 대충 분산값 정도의 오차범위 내에 있는 경우가 95%정도라고 생각하시면 되겠습니다.
표는 붙여넣을 줄을 몰라서 엑셀에서 복사해 넣었습니다.

코드는 아래와 같습니다. 지적할점 있으면 지적해주세요

%% howmanygames.m %%
function [avg, dev] = howmanygames(crank, desire, rate, numsim)  % current rank, desired rank, win rate, number of simulation
starreq = [5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 3 3 3 3 3 3 3 3 3 3];    % required star list
num = 0;    % number of games for one simulation
game(numsim) = 0; % saving number of games for each simulation times
for i = 1:numsim    % repeat simulation for numsim times
    rank = crank;   % rank used in simulation
    star = 0;   % current star number
    conswin = 0;  % consecutive wins, if it is more than 2, gain 2 star
    while rank > desire  % repeat until get to desired rank
        win = rand();
        if win < rate % if win
            conswin = conswin + 1;
            if ( rank > 5 && conswin >= 2) % consecutive winning bonus
                star = star + 2;
            else % no consecutive winning bonus
                star = star + 1;
            end
            if ( star > starreq(rank) ) % rank up
                star = star - starreq(rank);
                rank = rank - 1;
            end
        else % if lose
            conswin = 0;    % reset the consecutive wins
            if ~((rank == 20 && star == 0) || rank > 20)   % if rank = 20 and star = 0, or, rank is lower than 20, no penalty
                star = star - 1;
            end
            if (star < 0) % rank down
                rank = rank + 1;
                star = starreq(rank) - 1;
            end
        end
        num = num + 1; % count simulation
    end
    game(i) = num; % after simulation, save the repetation times
    num = 0; % reset the number of games
end
avg = mean(game); % get average number of games
dev = std(game);


%% simulation.m %%
for j = 1:5
    for i = 40:100
        [avg(i-39,j),dev(i-39,j)] = howmanygames(30-5*j,25-5*j,0.01*i,100);
    end
end

평균 25->20 20->15 15->10 10->5 5->전설
40% 30.12 231.92 1103.44 3300.89 26603409
41% 28 172.38 716.05 1668.42 3791194.9
42% 27.61 142.17 365.23 771.05 612269.26
43% 28.29 132.08 342.57 543.94 140147.29
44% 28.24 101.28 270.91 396.21 49652.75
45% 24.92 96.89 193.41 263.59 17920.03
46% 23.63 83.24 156.45 209.43 9422.17
47% 24.6 75.09 133.97 154.28 3877.29
48% 23.61 68.37 113.31 134.22 2175.64
49% 22.41 58.86 92.5 126.71 1285.69
50% 22.07 52.25 89.85 107.8 767.37
51% 22.7 49.17 76.71 92.9 543.9
52% 20.99 48.53 68.08 87.76 374.3
53% 21.8 42.38 58.03 80.37 317.92
54% 20.88 41.5 54.53 70.69 249.75
55% 19.17 37.66 51.18 70.68 221.3
56% 19.24 38.69 47.24 62.18 171.3
57% 18.92 32.49 43.1 56.61 165.59
58% 18.52 30.7 45.56 50.89 154.36
59% 18.44 32.3 41.83 46.72 137.17
60% 17.4 29.19 38.27 45.44 122.46
61% 16.89 26.39 37.84 47.39 116.22
62% 16.26 26.31 34.44 43.77 102.1
63% 16.44 22.37 32.24 43 93.89
64% 16.18 24.24 31.47 41.19 89.51
65% 15.34 21.83 30.35 37.51 83.98
66% 15.59 21.32 30.51 34.19 72.99
67% 15.03 20.74 28.03 35.74 70.24
68% 14.23 20.2 27.34 33.92 72.02
69% 14.31 19.99 25.66 32.64 64.41
70% 14.28 18.83 23.81 31.33 60.54
71% 14.12 18.33 23.2 29.06 61.78
72% 14.19 16.96 22.63 27.73 59.97
73% 13.77 17.99 21.92 28.27 55.89
74% 13.13 16.17 20.86 25.86 52.89
75% 13.26 16.64 20.64 24.75 50.97
76% 12.94 14.64 20.45 25.74 50.19
77% 12.92 14.79 19.95 23.16 46.86
78% 12.34 14.66 17.85 23.09 46.43
79% 12.3 14.51 18.13 22.79 44.51
80% 11.64 13.17 17.86 21.69 43.83
81% 12.06 13.24 17.29 21.6 40.72
82% 11.3 13.46 16.83 21.67 40.2
83% 11.15 12.69 16.24 19.84 39.11
84% 11.01 12.74 15.75 20.92 37.9
85% 11.06 11.9 15.35 19.57 37.01
86% 11.08 11.49 15.08 18.38 36.17
87% 10.66 11.75 14.83 18.04 34.08
88% 10.65 11.73 14.81 17.67 34.32
89% 10.4 11.26 13.72 18.47 33.17
90% 10.25 11.06 13.85 16.98 32.75
91% 10.14 11.01 13.85 16.73 32.01
92% 9.87 10.62 12.73 16.65 31.44
93% 9.56 10.25 12.54 16.08 30.11
94% 9.81 10.14 12.57 16.21 29.62
95% 9.58 9.98 12.18 15.42 28.88
96% 9.41 10.13 11.9 15.05 28.29
97% 9.32 9.55 11.7 14.79 27.45
98% 9.19 9.46 11.38 14.52 27.18
99% 9.1 9.08 11.32 14.26 26.52
100% 9 9 11 14 26
분산 25->20 20->15 15->10 10->5 5->전설
40% 9.251022 186.0506 1179.182 3810.338 29088818
41% 8.43753 164.9242 613.4402 1825.681 3582608.9
42% 8.770814 120.9715 327.9974 719.001 667425.17
43% 7.557584 96.31741 301.1052 541.1597 146635.25
44% 8.025364 77.31597 279.6572 364.8653 46009.271
45% 6.95974 80.8369 229.7529 255.4821 16189.768
46% 6.778837 54.88466 127.2731 147.2424 8114.5276
47% 6.531973 48.5345 110.9071 100.5428 3236.432
48% 6.788128 41.76453 83.72378 104.0431 1783.3175
49% 6.092046 37.3309 51.2785 70.44868 1286.9761
50% 5.857319 38.56894 58.06899 70.88417 670.70942
51% 6.566674 34.76532 45.07979 45.1705 573.01719
52% 5.977221 31.4827 39.69166 49.43379 242.726
53% 7.269696 22.60946 33.91506 38.84745 211.59336
54% 5.278659 25.80365 29.7102 29.55929 135.44169
55% 4.752575 22.49769 30.28797 33.44734 119.85989
56% 5.187631 23.70556 20.62073 25.25241 86.102652
57% 5.102346 17.85548 18.49297 22.52984 74.875398
58% 5.162961 16.18985 24.85567 23.09615 74.813778
59% 5.025832 14.01478 17.61545 19.69212 63.265842
60% 4.526019 15.08809 18.04867 16.18412 50.989882
61% 4.306197 11.30826 16.62888 22.89232 43.606732
62% 3.896696 13.02266 15.30915 18.80353 39.448854
63% 3.934617 9.780061 12.7874 16.0542 29.565163
64% 4.205288 11.56459 11.45431 15.71764 28.873984
65% 4.374444 8.190972 11.10953 16.52454 23.918625
66% 3.787526 9.214481 12.32022 11.9694 23.033789
67% 4.21746 7.989412 10.36452 11.51075 22.297402
68% 3.250035 7.775902 11.64302 10.7869 18.276209
69% 3.050733 8.627528 9.293443 10.75768 17.3689
70% 3.437993 7.06257 9.722093 9.437787 14.951382
71% 3.726184 6.313454 7.314024 8.296062 18.190118
72% 3.252645 6.188814 8.509834 7.614254 15.79634
73% 3.375057 7.74922 8.134824 7.647347 13.492905
74% 3.151575 5.130725 7.229317 6.872607 14.223963
75% 2.83064 5.977263 6.014326 6.019119 12.061672
76% 2.681097 4.493587 6.717045 6.685761 10.07617
77% 3.014041 4.42284 6.630775 5.6813 10.12749
78% 2.499374 5.444337 5.339722 5.419931 9.6392034
79% 2.552479 4.217556 5.036543 5.648447 7.4718259
80% 2.37206 3.104428 5.152267 4.78992 7.8032045
81% 2.268894 3.562203 4.610692 5.130617 6.8077199
82% 2.148525 3.630455 4.15861 5.406795 6.4620102
83% 1.966256 3.509568 3.934104 4.208505 6.9002928
84% 2.100962 3.230599 3.769562 4.529767 6.7442456
85% 1.999091 2.743219 3.7183 3.942145 6.6294339
86% 2.09704 2.676101 3.308025 3.74646 6.5828903
87% 1.695031 2.367179 3.556343 2.919527 4.8088303
88% 1.695955 2.791962 3.362584 3.041979 5.0169007
89% 1.414214 2.325181 2.534928 3.872866 4.6035889
90% 1.380784 2.178035 2.865239 2.700468 4.0957603
91% 1.370836 2.254042 2.705681 2.514021 3.7050198
92% 1.203152 2.205503 2.13132 2.426183 3.3403562
93% 0.902522 1.665909 1.966487 2.063047 3.0613096
94% 1.195066 1.497608 2.118962 2.080671 2.9568616
95% 0.996762 1.563343 1.659865 1.89726 2.4793287
96% 0.779731 1.360741 1.314257 1.486471 1.9451831
97% 0.601009 1.067187 1.251262 1.216843 1.7602514
98% 0.394277 0.947298 0.972137 0.881688 1.4451591
99% 0.301511 0.367355 0.839432 0.733333 0.9372213
100% 0 0 0 0 0