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]
Show older comments
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.
Accepted Answer
More Answers (2)
Frantisek Gaspar
on 6 Dec 2022
Edited: Torsten
on 6 Dec 2022
%% 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")
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
Categories
Find more on Lengths and Angles in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

