create a random two dice product ? histogram ?
이전 댓글 표시
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
Joseph Cheng
2015년 9월 18일
so whats the question or error?
답변 (1개)
Kirby Fears
2015년 9월 18일
편집: Kirby Fears
2015년 9월 18일
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
rana alamri
2015년 9월 18일
So there was no need for the loop ? thank you
Kirby Fears
2015년 9월 18일
Most vector/matrix operation in matlab won't require a loop unless you are treating each element differently. Glad to help.
카테고리
도움말 센터 및 File Exchange에서 Descriptive Statistics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!