I cannot see anything wrong with that code. On the contrary. It's an elegant implemetation.
When you say the issue happens randomly, is this true for any of the 'cases' iteration? I suppose the way forward is to try and figure out if the randomness is really random or if there is a pattern to it. One idea, if you haven't already tired it, is to solve the simulation for one case only, but wrap it in an outer loop to repeat the same simulation several times (e.g. 100 depending on the run times). This should help you isolate the issue to either infrastructure, in case stalling is still random, or problem formulation, if the stalling only happens for one or some cases. If the later you'll also have a better indication of where to dig further to identify the real issue.
... View more