필터 지우기
필터 지우기

Why isn't my figure consistent? It keeps changing every time

조회 수: 2 (최근 30일)
Kevin Nelson
Kevin Nelson 2022년 9월 8일
댓글: Kevin Nelson 2022년 9월 8일
numDays = 365;
numTrials = 1000;
maxPeople = 80;
pn = zeros(maxPeople, 1);
for numPeople = 1:maxPeople
for i = (1:numTrials)
probability = 1-(1-((numPeople-1)/randi(numDays)));
end
pn(numPeople) = probability;
end
%%
figure;
ax = axes;
plot(pn)
xlabel('Value')
ylabel('Probability')
  댓글 수: 2
Kevin Nelson
Kevin Nelson 2022년 9월 8일
I'm supposed to get an answer in which pn(22) > 0.5, but everytime I generate the code the number is too far above it, below, or exactly 0.5. Why?
Kevin Nelson
Kevin Nelson 2022년 9월 8일
I mean that it should be 0.5 from 22 through 80

댓글을 달려면 로그인하십시오.

답변 (1개)

Steven Lord
Steven Lord 2022년 9월 8일
Here's one line of your code. I've put it in block comments so MATLAB Answers won't try to execute it, since I have other code later in the answer I want to run and this line throwing an error because numPeople and numDays are not defined would prevent that.
%{
probability = 1-(1-((numPeople-1)/randi(numDays)));
%}
Since this line of code calls randi, which generates pseudorandom numbers, it's not surprising that the contents of the variable probability are different each time this line runs. If you rolled a 6-sided die over and over and always ended up with a 2, you'd be surprised (and perhaps a little angry at the person that gave you the die, for giving you a loaded die.)
n = 10;
rolls = zeros(1, n);
for whichroll = 1:n
rolls(whichroll) = randi(6);
end
rolls
rolls = 1×10
4 2 3 2 4 5 5 1 6 6
Yes, I know I could have generated them all at once. And these rolls won't be the same as the previous ones.
otherrolls = randi(6, 1, n)
otherrolls = 1×10
2 1 5 3 1 5 4 4 3 6
  댓글 수: 3
Steven Lord
Steven Lord 2022년 9월 8일
I can't answer either of your questions because I'm not sure what problem you're trying to solve. Can you state in words (no code, no math) the problem you're trying to solve and what you intend pn and probability to represent? My first guess was that you're trying to study or simulate the birthday problem but I'm not sure.
Kevin Nelson
Kevin Nelson 2022년 9월 8일
It is the birthday problem using the equation for probability. I'm supposed to be receiving a probability of >0.5 from 22 - 80, like pn(55) should be > 0.5

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by