create a random two dice product ? histogram ?
Show older comments
the question say :
given a dice with six numbers ({1, 2, 3, 4, 5, 6}), each number comes with the same probability when you roll it. Here is the game. Suppose you have such TWO dices and you simultaneously roll both of them to get the product of the two output numbers. When the product is 1 or 36, we say that you get the magic numbers and you will be rewarded. However, each play will cost you a certain amount of money and you can only afford to play 100 times. Let the random variable X denote the total number of times you will hit those magic numbers and be rewarded. Image you are repeating this game a 1000 times, and counting how many times have you won during each game. You have to show the distribution of getting the magic numbers by plotting the histogram.
and this is the code i have so far ..
n=1000 ;
x = randi(6,n,1);
y = randi(6,n,1);
distr = zeros (6,1) ;
count1= 0 ;
for k = 1 : n
ProductOf = x(k)*y(k);
if (ProductOf== 1 || ProductOf == 36 )
count1 = count1 + 1 ;
end
end
Bins = (2:6 * 2);
histogram(count1, Bins);
hold on
1 Comment
Joseph Cheng
on 18 Sep 2015
so whats the question or error?
Answers (1)
Kirby Fears
on 18 Sep 2015
Edited: Kirby Fears
on 18 Sep 2015
Hi rana,
If you sample dice d1 & d2 1000 times each and multiply them together, you have a sample space of 1000 products. Do you want to plot the histogram of these products with 36 separate bars? It would show a count of how often each product appeared out of 1000 total. Below is a simple code for that.
n=1000; % Size of sample
x = randi(6,n,1);
y = randi(6,n,1);
prod=x.*y;
histogram(prod,0:1:35);
If you are trying to make a histogram of the random variable X, you might be looking for a different histogram. This sample space of 1000 products would yield only 1 observation of X, defined as the number of 1's and 36's that appear in your 1000-point sample space.
To make a histogram of X, you'd need to repeat your experiment many times. Meaning you need N sample spaces, each with 1000 products and a single X measurement, to create a histogram of X measured N times where each measurement has a sample space of 1000 dice rolls.
The code below makes such a histogram of X.
n=1000; % Size of each sample
rep=100; % Repetitions of X measurement
x = randi(6,n,rep);
y = randi(6,n,rep);
prod=x.*y;
X=sum(prod==1 | prod==36);
histogram(X);
You can freely alter rep to your needs.
Hope this helps.
2 Comments
rana alamri
on 18 Sep 2015
So there was no need for the loop ? thank you
Kirby Fears
on 18 Sep 2015
Most vector/matrix operation in matlab won't require a loop unless you are treating each element differently. Glad to help.
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!