simulating rolling 1 and 2 dice

9 views (last 30 days)
Laura Dunn
Laura Dunn on 28 Jan 2025
Moved: Torsten on 28 Jan 2025
I need to simulate rolling one dice 10 and 1000 times, and two dice 10 and 1000 times. I must add together the sums for the two dice. I then need to create a histogram with the PDF and CDF. I used a kernel density estimation to smooth the curves. I want to know if my code can be created into a loop to make things simpler. Here is what I have for the rolling 1 dice:
% Simulate 10 rolls, 1 die
roll_10 = randi([1, 6], 10, 1); %using the 1 because only rolling 1 die
mean_10=mean(roll_10);
std_10=std(roll_10);
figure;
histogram(roll_10)
% Plot the smooth PDF using a kernel density estimation
figure;
subplot(2, 1, 1);
ksdensity(roll_10); % Smooth PDF using kernel density estimation. each data point replaced with a wieghing function to estimate the pdf
title('Smooth PDF of Dice Rolls');
xlabel('Dice Face Value');
ylabel('Probability Density');
xlim([0, 7]); % Limiting x-axis to dice face values
% Plot the smooth CDF using a kernel density estimation
subplot(2, 1, 2);
ksdensity(roll_10, 'Cumulative', true); % Smooth CDF
Error using internal.stats.parseArgs (line 43)
Invalid parameter name: Cumulative.

Error in mvksdensity>parse_args (line 209)
[u,isXChunk] = internal.stats.parseArgs({'width','isXChunk'},{u,isXChunk},extra{:});

Error in mvksdensity (line 89)
support,weight,cens,cutoff,bdycorr,ftype,plottype,isXChunk] = parse_args(yData,varargin{:});

Error in ksdensity (line 235)
[fout,xout,u,plottype] = mvksdensity(yData,xi,varargin{:});
title('Smooth CDF of Dice Rolls');
xlabel('Dice Face Value');
ylabel('Cumulative Probability');
xlim([0, 7]); % Limiting x-axis to dice face values

Answers (2)

Voss
Voss on 28 Jan 2025
% define a function that creates the plots for a given set of results
% (roll) and corresponding number of dice (n_dice) and number of rolls
% (n_rolls)
function plot_roll(roll,n_dice,n_rolls)
figure;
subplot(1, 2, 1);
histogram(roll)
title(sprintf('%d dice, %d rolls',n_dice,n_rolls))
% Plot the smooth PDF using a kernel density estimation
subplot(2, 2, 2);
ksdensity(roll); % Smooth PDF using kernel density estimation. each data point replaced with a wieghing function to estimate the pdf
title('Smooth PDF')
xlabel('Dice Face Value');
ylabel('Probability Density');
xlim([0, 6*n_dice+1]); % Limiting x-axis to dice face values
% Plot the smooth CDF using a kernel density estimation
subplot(2, 2, 4);
% ksdensity(roll, 'Cumulative', true); % Smooth CDF
ksdensity(roll, 'Function', 'cdf'); % Smooth CDF
title('Smooth CDF')
xlabel('Dice Face Value');
ylabel('Cumulative Probability');
xlim([0, 6*n_dice+1]); % Limiting x-axis to dice face values
end
% simulate the dice rolls, sum, and plot results
for n_dice = [1 2]
for n_rolls = [10 1000]
% Simulate n_rolls rolls with n_dice dice
roll = randi([1, 6], n_rolls, n_dice);
% sum across dice
roll = sum(roll,2);
% plot the results
plot_roll(roll,n_dice,n_rolls)
end
end

Torsten
Torsten on 28 Jan 2025
Moved: Torsten on 28 Jan 2025
You get a discrete pdf and cdf for the one and two dice-roll experiments you describe. So it doesn't make sense to smoothe results and generate values between the integers 1,2,3,4,5 and 6 resp. 1,2,3,4,5,6,7,8,9,10,11 and 12 using the ksdensity function.
Using
figure()
histogram(roll_10,'Normalization','pdf')
figure()
histogram(roll_10,'Normalization','cdf')
is the best you can do.

Community Treasure Hunt

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

Start Hunting!