- Let you specify a range of integers (e.g. 1 to 10).
- Randomly draw N numbers from that range (with or without replacement).
- Display each draw.
- Repeat the experiment M times to estimate the probability of each possible number appearing in the draw.
below i have attached the code, please take it as reference and modify it accordingly:
%% SETTINGS
minVal = 1; % Minimum in range
maxVal = 10; % Maximum in range
Ndraw = 3; % How many numbers drawn each trial
withRepl = false; % true = allow repeats, false = no repeats
Mtrials = 10000; % How many experiments to run
%% PREPARE
values = minVal:maxVal;
K = numel(values);
countHits = zeros(1,K); % count of times each value was drawn
allDraws = zeros(Mtrials,Ndraw); % record of every draw
%% RUN EXPERIMENTS
for t = 1:Mtrials
if withRepl
% Draw Ndraw samples with replacement
draw = values(randi(K,1,Ndraw));
else
% Draw Ndraw samples without replacement
draw = values(randperm(K,Ndraw));
end
allDraws(t,:) = draw;
% Count each drawn value
for v = draw
idx = v - minVal + 1; % index into countHits
countHits(idx) = countHits(idx) + 1;
end
end
%% DISPLAY ONE EXAMPLE
exampleRun = 1; % Change to any trial index 1..Mtrials
fprintf('Example draw #%d: ', exampleRun);
fprintf('%d ', allDraws(exampleRun,:));
fprintf('\n\n');
%% ESTIMATE PROBABILITIES
% Each trial draws Ndraw numbers, so total draws = Mtrials*Ndraw
totalDraws = Mtrials * Ndraw;
probEst = countHits / totalDraws;
% Show results in a table
T = table(values.', countHits.', probEst.', ...
'VariableNames',{'Value','Count','EstimatedProbability'});
disp(T);
%% PLOT THE DISTRIBUTION
figure;
bar(values, probEst);
xlabel('Value drawn');
ylabel('Estimated Probability');
title(sprintf('Estimated Probability of Drawing Each Value (%d trials × %d draws)', Mtrials, Ndraw));
grid on;