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
AI Thread Summary
The discussion focuses on creating a program to simulate rolling a fair six-sided die and calculating the percentage distribution of the results. Users are prompted to enter the number of rolls, with validation to prevent illegal inputs. The initial code attempts to generate random numbers but fails to ensure a fair distribution due to rounding issues. Suggestions include using the ceil or floor functions to correctly generate numbers between 1 and 6. Ultimately, the program should count each result and display both the counts and percentage distributions accurately.
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

Back
Top