using a Monte Carlo simulation with 100,000 trials to find the probability of a random integer that is larger than the other random integer in [4,21]

6 次查看(过去 30 天)
trials= 100000; % 100,000 trials
A_win=0;
A_win_or_equals_to_B=0;
for i = 1 : trials
% min 4 and max 21. a random integer bewteen 4 and 21
A_value= randi([4,21]);
B_value= randi([4,21]);
if A_value > B_value
A_win=A_win+1;
elseif A_value >= B_value
A_win_or_equals_to_B= A_win_or_equals_to_B+1;
end
end
prob_A_win= A_win / trials;
prob_A_win_or_equals_to_B= A_win_or_equals_to_B / trials;
% blue, player win
plot(A_value,prob_A_win,'blue')
xlabel('A value')
ylabel('Probability')
hold on
plot(A_value,prob_A_win_or_equals_to_B,'red')
legend('Probability A wins','Probability A wins or equal to B')
A and B are both random integers from the interval [4, 21]. I want to run a Monte Carlo simulation with 100,000 trials to find the probabilities that
A > B, and A >= B. I think the right code is supposed to plot a figure with a blue line and a red line, and there should be 100,000 A and B values. However, my codes appear to generate only one A and one B value.

采纳的回答

David Hill
David Hill 2022-12-6
trials= 100000; % 100,000 trials
A=repmat((4:21)',1,trials);
B=randi([4 21],18,trials);
A_win=sum(A>B,2);
A_win_or_equals_to_B=sum(A>=B,2);
prob_A_win= A_win / trials;
prob_A_win_or_equals_to_B= A_win_or_equals_to_B / trials;
plot(4:21,prob_A_win,'blue',4:21,prob_A_win_or_equals_to_B,'red');
xlabel('A value')
ylabel('Probability')
legend('Probability A wins','Probability A wins or equal to B')

更多回答(2 个)

Frantisek Gaspar
Frantisek Gaspar 2022-12-6
编辑:Torsten 2022-12-6
%% Parameters
I_min = 4;
I_max = 21;
n_trials = 1e4;
%% Simulation
A_value = I_min:I_max;
B_random_value = randi( ...
[I_min, I_max], ...
n_trials, numel(A_value));
prob_A_win = mean(A_value > B_random_value);
prob_A_win_or_equals_to_B = mean(A_value >= B_random_value);
%% Plot
figure()
hold on
plot(A_value, prob_A_win, "blue", ...
"DisplayName", "Probability A wins")
plot(A_value, prob_A_win_or_equals_to_B, "red", ...
"DisplayName", "Probability A wins or equal to B")
xlabel('A value')
ylabel('Probability')
box on
grid on
xlim([I_min, I_max])
legend("Location", "best")

Torsten
Torsten 2022-12-6
编辑:Torsten 2022-12-6
A > B, and A >= B. I think the right code is supposed to plot a figure with a blue line and a red line, and there should be 100,000 A and B values. However, my codes appear to generate only one A and one B value.
I think that it is correct that only one value for the probability A>B resp. A>=B should be generated.
According to my interpretation, you draw two random integers from 4:21 and search for the probability that the first drawn number is greater (greater equal) compared to the second. This gives one value as probability.
p(A>B) = (0+1+2+...+17)/(18*18) = (17*18/2) /(18*18) = 17/36
p(A>=B) = (1+2+...+18)/(18*18) = (18*19/2) / (18*18) = 19/36

类别

Help CenterFile Exchange 中查找有关 Frequently-used Algorithms 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by