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 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
0 commentaires
Réponse acceptée
David Hill
le 6 Déc 2022
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 commentaires
Plus de réponses (2)
Frantisek Gaspar
le 6 Déc 2022
Modifié(e) : Torsten
le 6 Déc 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")
0 commentaires
Torsten
le 6 Déc 2022
Modifié(e) : Torsten
le 6 Déc 2022
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
0 commentaires
Voir également
Catégories
En savoir plus sur Frequently-used Algorithms dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!