Need help to create a function for a roulette simulation?

12 ビュー (過去 30 日間)
Steven Thies
Steven Thies 2021 年 2 月 12 日
コメント済み: Jan 2021 年 2 月 13 日
My function does not work and i don't know how to link my input number n with the simulation of the roulette.
There are 18 red, 18 black and 1 green fields. If the ball lands on the field which the user choose then he gets the money back and gets the same amount of money which he used. if the ball lands not on the field which the user choose then he loose his money.
function [red, black] = n_montecarlo(n)
red = [3 12 7 18 9 14 1 16 5 23 30 36 27 34 25 21 19 32];
black = [26 35 28 29 22 31 20 33 24 10 8 11 13 6 17 2 4 15];
roulette = randi(37);
for n = red
if roulette == n
disp('win')
else
disp('lose')
for n = black
if roulette == n
disp('win')
else
disp('lose')
end
end
end
end
end

採用された回答

Jan
Jan 2021 年 2 月 13 日
"If the ball lands on the field which the user choose"
How does thze user choose a field? By the input n? This input is overwritten by
for n = red
Perhaps you want:
function n_montecarlo(n)
red = [3 12 7 18 9 14 1 16 5 23 30 36 27 34 25 21 19 32];
black = [26 35 28 29 22 31 20 33 24 10 8 11 13 6 17 2 4 15];
roulette = randi(37);
if n == roulette
disp('win')
else
disp('lose')
end
if any(n == red)
disp('red')
elseif any(n == black)
disp('black')
else
disp('green')
end
end
  6 件のコメント
Steven Thies
Steven Thies 2021 年 2 月 13 日
編集済み: Jan 2021 年 2 月 13 日
This is my Script. The function is n_montecarloreal(n) which i have already.
n = input('please choose a number.For red(3,12,7,18,9,14,1,16,5,23,30,36,27,34,25,21,19,32),black(26,35,28,29,22,31,20,33,24,10,8,11,13,6,17,2,4,15): ','s');
while isnan(str2double(n)) == true
n = input('Error.Please again ','s');
end
n = str2double(n);
m = n_montecarloreal(n);
output = (num2str(m));
disp(output)
function n_montecarloreal(n)
red = [3 12 7 18 9 14 1 16 5 23 30 36 27 34 25 21 19 32];
black = [26 35 28 29 22 31 20 33 24 10 8 11 13 6 17 2 4 15];
roulette = randi(37);
if n == roulette
disp('win')
else
disp('lose')
end
if any(n == red)
disp('red')
elseif any(n == black)
disp('black')
else
disp('green')
end
end
Jan
Jan 2021 年 2 月 13 日
Your function n_montecarloreal(n) does not have an output yet. Maybe all you need is:
function Won = n_montecarloreal(n)
roulette = randi(37);
Won = (roulette == n);
end

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeBoard games についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by