How to use runif in loop to and categories off runif function

302 Views Asked by At

The question is:

John either visits his girlfriend who lives downtown, or visits his mother who lives uptown, but not both. In order to be completely fair, he goes to the bus stop every evening at a random time between 6pm and 7pm, and takes either an uptown or a downtown bus (from the same bus stop), whichever comes first. Each of the two kinds of buses arrives at the bus stop every 30 minutes with a fixed regular schedule: the arrival times for downtown buses are a or (30 + a) minutes past each hour, and that for uptown buses are b or (30 + b) minutes past each hour, where 1 ≤ a < b < 30 are two integers.

Write an R-function with a and b as input variables to simulate John’s experience at the bus station every evening.

My approach to solving this is defining a and b as something arbitrary e.g

a=10 b=10

then defining John arrival time JATime <- runif(n=30, min=0,max=59)

then want to use if statements for the criteria to be met e.g. if(JATime <= a && b < JATime <= 30+a && JATime > 30+b) then GF, else Mum.

I want to use a loop to run it 30time (to simulate a month) and calculate number times visits mum vs GF to compare to expected probability of b-a.

But the issue I'm having is R is not accepting the inequalities and also I feel like there are more errors in the code.

Can anyone help?

1

There are 1 best solutions below

0
On

Your second inequality is incorrect. You need to break apart the inequality as two separate inequalities:

b < JATime && JATime <= 30 + a

Notice that the second of these does not need to be used based off your first inequality:

If JATime <= a, then JATime <= 30 + a.

So really, you can just change the second inequality to

b < JATime