Stuart’s MATLAB Videos

Watch and Learn

MATLAB Puzzler: Professor and the Rain

Being a company full of geeks, a fair amount of brainteasers get passed around. One that recently came around got my attention. I knew there was probably math that governed it elegantly, but I did not know how to do that. I do know how to brute force something in MATLAB, so I wrote a monte carlo simulation.

I am against premature optimization. I think you should write clear, easy to maintain code and then and only then optimize if it really needs it. In this case, I needed to optimize to get the code to run in a reasonable time. Once you all have had a chance to solve this puzzler in the comments, I will make a video showing the progression from code that was written quickly to code that was optimized for speed. Until then, try this puzzler out:

A professor has an umbrella in the office and another one at home.

If it is raining when the professor goes out, he takes an umbrella with him, 
if one is available.  If it is not raining, he doesn't carry any umbrellas 
with him.  Whenever the professor walks with an umbrella, he stays dry. 
Whenever he walks without an umbrella in the rain, he gets wet.

Given p = probability of rain (same on every trip), what is the (steady 
state) probability of getting wet?

I.e., what is the probability of rain & no available umbrella?
I heard about this problem during Jeff Buzen's talk at the joint IEEE/ACM 
meeting, The Improbable Success of Probabilistic Models.
The problem is taken from the Introduction to Probability textbook by MIT 
professors D.P. Bertsekas and J. N. Tsitsklis. 

As always with these puzzlers, be sure to use the proper tags in the comments.

<pre> <code>
[deltaX, deltaY] = puzzler(inMatrix)
%% All the code so someone can just copy and paste it from the comments.
</code> </pre>
  • print


To leave a comment, please click here to sign in to your MathWorks Account or create a new one.