0
numtrials = 1000000
turkeychance= 0
for i = numtrials
    for b = 1:10
    r = rand(10,1);
    if r(b)<=.3143

I'm trying to use the monte Carlo simulation method to find the probability of an event occurring within a list of 10 three times in a row. I'm going to run 1 million trials. The probability of a the event occurring is 31.43 percent at any time. What I'm thinking is I'll call out any trial (b) and create a nested loop so If condition one (rand value is less than .3143) I'll move to the next number in the index and if that number is less than .3143 I'll move on to the the next number. If this occurs I'll add 1 to turkey chance. When the million trials are done I'll divide turkeychance by 1 million to get the probability of the event occurring three times in a row. My question is, how can I check if the event occurs three times in a row? Forgive me if I did not provide enough info, I'm new to programming.

1 Answers1

0

If you define

r = rand(10,1);

then you don't need the inner for loop. You can detect your events using

events = r<0.3143;

and you can find if there are 3 or more in a row by doing

Times = events;
Times(3:end) = Times(3:end) + events(2:end-1);
Times(3:end) = Times(3:end) + events(1:end-2);

Note that in 10 times you can have 3 events in a row more than once. Use sum(Times>3) to count how many are there or any(Times) if you only interested in yes or no answer.

ThP
  • 2,312
  • 4
  • 19
  • 25