What is the percentage distribution of dots rolled on a fair six-sided die?

  • Thread starter Thread starter zack7
  • Start date Start date
  • Tags Tags
    Programming
Click For Summary

Discussion Overview

The discussion revolves around simulating the rolling of a fair six-sided die using MATLAB. Participants explore how to implement a program that counts the percentage distribution of the results from multiple rolls, addressing issues related to generating fair random numbers and the structure of the code.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents a MATLAB script to simulate rolling a die and asks for the expected percentage distribution of the results.
  • Another participant critiques the random number generation method, arguing that rounding the output of "rand*5+1" leads to an unfair distribution of outcomes.
  • Some participants suggest using "ceil" or "floor" functions instead of rounding to achieve a fair die roll.
  • Concerns are raised about the structure of the loops in the provided code, with suggestions to simplify the counting mechanism for the die rolls.
  • Multiple participants note that the counters for each die face (d1 to d6) are not being incremented correctly in the code.

Areas of Agreement / Disagreement

There is no consensus on the correct implementation of the die simulation. Participants express differing views on the appropriate method for generating fair random numbers and the structure of the code. The discussion remains unresolved regarding the optimal approach.

Contextual Notes

Participants highlight limitations in the initial code, including issues with random number generation and the need for proper incrementing of counters. There are also unresolved questions about the correct implementation of loops and conditions in the program.

zack7
Messages
52
Reaction score
0

Homework Statement


Write a program to simulate rolling a six-sided “fair” die with one dot on one side, two dots on another side, three dots on another side, and so on. Allow the user to enter the number of rolls. Use first while loops to trap the illegal input number first (negative, zero, or float numbers). Use second loop to count the percentage distribution of each dot in the pay. Print the number of rolls that gave one dot, the number of rolls that gave two dots, and so on.
What should be the percentage distribution of the number of dots from the rolls?

2. Relevant condtions
1) use rand function and ceil or floor function to generate a whole number between 1-6
2) Use while loop and switch-case statement to count the times of each number in the play.
3) Use fprintf to display the number of dots generated and the percent distribution of number of dots from the rolls.
4) Trying to give a larger input number to loop more times. What should be the percentage distribution of each dot(s) on each side from the rolls?


The Attempt at a Solution


clc
clear

%User input
user=input('How many times would you want to roll the dice ? :');

%Error Statement
while user <=0 || mod(user,2)~=0 || mod(user,2)~=1;
disp('Error input,Please try again');
user=input('How many times would you want to roll the dice ? :');
end

% Intilization
counter=0;
d1=0;
d2=0;
d3=0;
d4=0;
d5=0;
d6=0;

%To determine the number of dots
while counter< user;
count=round(rand*5+1)
switch count
case (1)
d1=fprintf('The number of one dot is %2.f \n ',d1)
percentage1=count/user*100
percentage1=fprintf('The percentage of heads gotten is: %.2f \n' ,percentage1);
case (2)
d2=fprintf('The number of two dot is %2.f \n ',d2)
percentage2=count/user*100
percentage2=fprintf('The percentage of heads gotten is: %.2f \n' ,percentage2);
case (3)
d3=fprintf('The number of three dot is %2.f \n ',d3)
percentage3=count/user*100
percentage3=fprintf('The percentage of heads gotten is: %.2f \n' ,percentage3);
case (4)
d4=fprintf('The number of three dot is %2.f \n ',d4)
percentage4=count/user*100
percentage4=fprintf('The percentage of heads gotten is: %.2f \n' ,percentage4);
case (5)
d5=fprintf('The number of three dot is %2.f \n ',d5)
percentage5=count/user*100
percentage5=fprintf('The percentage of heads gotten is: %.2f \n' ,percentage5);
otherwise
d6=fprintf('The number of three dot is %2.f \n ',d6);
percentage6=count/user*100
end
end

Thank you for all the help
 
Physics news on Phys.org
zack7 said:
count=round(rand*5+1)
This will not generate a fair die. The quantity rand*5+1 will produce a number in the range 1-6. It sounds like it would be fair, but rounding the number will produce the following results:
1 (1 <= x < 1.5) - 10% of the time
2 (1.5 <= x < 2.5) - 20% of the time
3 (2.5 <= x < 3.5) - 20% of the time
4 (3.5 <= x < 4.5) - 20% of the time
5 (4.5 <= x < 5.5) - 20% of the time
6 (5.5 <= x <= 6) - 10% of the time

Instead, you could make it span 0.5-6.5 so that the rounding would create a fair die,
count=round(rand*6+0.5)

Edit: After rereading the question, I realized it said to use either the floor or ceil functions. It might be best to use one of those instead.
 
Last edited:
clc
clear

%User input
user=input('How many times would you want to roll the dice ? :');

%Error Statement
while user <=0 || mod(user,2)~=0 || mod(user,2)~=1;
disp('Error input,Please try again');
user=input('How many times would you want to roll the dice ? :');
end

% Intilization
count=0;
total_count=0;

%To determine the number of dots
while total_count<= user;
count=ceil(rand*5);
total_count=count+1;
switch count
case (count==1)
d1=fprintf('The number of one dot is %2.f \n ',d1);
percentage1=count/user*100;
percentage1=fprintf('The percentage of heads gotten is: %.2f \n' ,percentage1);
case (count==2)
d2=fprintf('The number of two dot is %2.f \n ',d2);
percentage2=count/user*100;
percentage2=fprintf('The percentage of heads gotten is: %.2f \n' ,percentage2);
case (count==3)
d3=fprintf('The number of three dot is %2.f \n ',d3);
percentage3=count/user*100;
percentage3=fprintf('The percentage of heads gotten is: %.2f \n' ,percentage3);
case (count==4)
d4=fprintf('The number of three dot is %2.f \n ',d4);
percentage4=count/user*100;
percentage4=fprintf('The percentage of heads gotten is: %.2f \n' ,percentage4);
case (count==5)
d5=fprintf('The number of three dot is %2.f \n ',d5);
percentage5=count/user*100;
percentage5=fprintf('The percentage of heads gotten is: %.2f \n' ,percentage5);
otherwise
d6=fprintf('The number of three dot is %2.f \n ',d6);
percentage6=count/user*100;
end
end

This is my new mathlab script after changes but it still does not work
 
zack7 said:
count=ceil(rand*5);
This still won't work. ceil(rand*5) will only produce integers 1-5.

In your 2nd while loop, shouldn't that only be counting the number of times each die has rolled? It seems like your 2nd while loop encompasses much more than you want it to.
In your program you set the counters d1-d6 to zero, yet you never incremented them. I think your code should be more along the lines of:
Code:
while total_count<= user;
count=ceil(rand*5);
total_count=count+1;
//increment the correct counter
end
//print out all of the averages

Since I've never scripted anything in matlab, I can't comment on the syntax all the much.
 
Ninty64 said:
This still won't work. ceil(rand*5) will only produce integers 1-5.

In your 2nd while loop, shouldn't that only be counting the number of times each die has rolled? It seems like your 2nd while loop encompasses much more than you want it to.
In your program you set the counters d1-d6 to zero, yet you never incremented them. I think your code should be more along the lines of:
Code:
while total_count<= user;
count=ceil(rand*5);
total_count=count+1;
//increment the correct counter
end
//print out all of the averages

Since I've never scripted anything in matlab, I can't comment on the syntax all the much.

Thank you very much, I got it figured out.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
6K
Replies
3
Views
2K
  • · Replies 67 ·
3
Replies
67
Views
16K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
8K