Matlab - Gra Hazardowa
Symulacja gry hazardowej (obstawianie aby dojsc do pewnej sumy)
disp('Symulacja gry "Gambling Table"');
game = input('Wybierz stol:
(1)-rzuty moneta (2)-rzuty kostka --> ');
taktyka = input('Grasz zachlannie(1)
czy ostroznie (2) --> ');
min = 1;
max = input('Na wstepie masz 1 zloty
ile chcesz wygrac --> ');
trials = input('Dla ilu prob
przeprowadzamy symulacje --> ');
tnows = 0; %total number of win steps
(sum from each interation-step)
tnols = 0; %total number of loses steps
(sum from each interation-step)
tnow = 0; %total number of wins
tnol = 0; %total number of loses
if (game == 1) if (taktyka == 1) for i=1:trials [win,steps] = zachlanna(min,max,0.5); if (win == 1) tnow = tnow + 1; tnows = tnows + steps; elseif (win == 0) tnol = tnol + 1; tnols = tnols + steps; end end elseif(taktyka == 2) for i=1:trials [win,steps] = ostrozna(min,max,0.5); if (win == 1) tnow = tnow + 1; tnows = tnows + steps; elseif (win == 0) tnol = tnol + 1; tnols = tnols + steps; end end end
elseif (game == 2) if (taktyka == 1) for i=1:trials [win,steps] = zachlanna(min,max,0.83333333); if (win == 1) tnow = tnow + 1; tnows = tnows + steps; elseif (win == 0) tnol = tnol + 1; tnols = tnols + steps; end end elseif (taktyka == 2) for i=1:trials [win,steps] = ostrozna(min,max,0.83333333); if (win == 1) tnow = tnow + 1; tnows = tnows + steps; elseif(win == 0) tnol = tnol + 1; tnols = tnols + steps; end end end end
format long
disp('Wygranych bylo: (w procentach)'); disp(100*tnow/trials);
disp('Dla sredniej liczby krokow rownej'); disp(tnows/tnow);
disp('Przegranych bylo: (w procentach)'); disp(100*tnol/trials);
disp('Dla sredniej liczby krokow rownej'); disp(tnols/tnol);
format short
clear
% zachlanna.m
function [wygral,kroki] = zachlanna(min,max,pp); %(pp) to poziom przejscia
rezultat = min;
kroki = 0;
while ((rezultat < max) & (rezultat >= min))
kroki = kroki + 1;
los = rand(1);
if (los >= pp)
if (rezultat < max)
rezultat = rezultat *2;
end
else
if (rezultat < ceil(max/2))
rezultat = min - 1;
else
rezultat = 2 * rezultat - max;
end
end
end
if (rezultat >= max )
wygral = 1;
elseif (rezultat < min)
wygral = 0;
end
end
% ostrozna.m function [wygral,kroki] = ostrozna(min,max,pp); %(pp) to poziom przejscia rezultat=min; kroki=0; while ((rezultat < max) & (rezultat >= min)) kroki = kroki + 1; los = rand(1); if (los >= pp) rezultat = rezultat + 1; else rezultat = rezultat - 1; end end if (rezultat >= max ) wygral = 1; elseif (rezultat < min) wygral = 0; end end
2012.11.22 22:29:17.