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

In summary: 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
  • #1
zack7
55
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
  • #2
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:
  • #3
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
 
  • #4
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.
 
  • #5
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.
 

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

What is programming help?

Programming help is assistance provided to individuals or groups who are learning or working on coding and programming projects. It can include tutoring, troubleshooting, and guidance on how to solve programming problems.

Where can I find programming help?

There are many resources available for programming help, including online forums, coding communities, and tutoring services. You can also find help through your school or workplace if you are a student or employee.

What programming languages can I get help with?

Programming help is available for a wide range of languages, including popular ones like Java, Python, and C++. However, the specific languages you can get help with may depend on the expertise of the person or service providing assistance.

How can I get the most out of programming help?

To get the most out of programming help, it is important to come prepared with specific questions or problems you need help with. Clear communication and active participation in the learning process will also help you get the most value from the assistance you receive.

Is programming help cheating?

No, programming help is not considered cheating as long as you are using it to learn and improve your skills. However, it is important to properly credit any resources or assistance you receive in your work.

Similar threads

Replies
7
Views
2K
Replies
2
Views
2K
Replies
5
Views
5K
2
Replies
67
Views
11K
Replies
2
Views
3K
Replies
4
Views
8K
Replies
2
Views
3K
Replies
1
Views
3K
Back
Top