I'm still new to Mathematica and this is the first time I've wanted to do something more complicated than plotting and changing parameters of mathematical functions.

I want to make a simple simulation of a bus station. Two SkewNormalDistributions with different parameters give the rates for arrivals and services. Basically, the program should draw a number from the arrival distribution, then draw a number from the service distribution, subtract it from the arrivals and go on to the next simulation step. If possible, each number should be represented graphically. For instance, drawing 20 from the arrival distribution could be represented as a list of 20 circles, or 20 *s.

As far as I know, this is something that could be made with Animate[] (and maybe Table[]), but I don't even know where to start. I thought of using a While[] loop and using Pause[] at the end of each step, but I don't think this is a good way of doing it.

Edit: Here's the procedural code of what I want to do:

arrivals = SkewNormalDistribution[30, 5, -4];
departures = SkewNormalDistribution[2, 1, 0];

 entries = Round[RandomReal[arrivals]];
 services = Round[RandomReal[departures]];
 remaining += entries - services;
 If[remaining < 0, remaining = 0];
 Print[entries]; Print[services]; Print[remaining]; Print["    "],
 {n, 10}
  • 2
    $\begingroup$ You obviously know very clearly what you want, whereas we don't. You'll get a much better response (perhaps even some response rather than none) if you write the code to the level of your current abilities and ask how it can be improved, rather than posing the problem in the abstract and asking how it can be solved. In fact, you might even figure it out for yourself along the way. :) $\endgroup$ Jun 23, 2013 at 0:52
  • $\begingroup$ I'll give it a try then. I still have to figure out the scoping rules of Mathematica, I hope they aren't too different from those of popular procedural languages like C. $\endgroup$ Jun 23, 2013 at 0:55
  • $\begingroup$ Well, Mathematica differs significantly from other languages, and this is one example of it. On the plus side, there's plenty of information about scoping constructs on this site. $\endgroup$ Jun 23, 2013 at 0:58
  • $\begingroup$ Thank you for the information on scoping. I've updated my answer with the code. $\endgroup$ Jun 23, 2013 at 1:07

1 Answer 1


Here's a way to rewrite your loop (without using a loop) by generating all n of the random numbers at once, then dealing with the basic calculation (which accumulates the number that haven't departed) in one command. The Clip makes sure the number of remaining never goes below zero and the Transpose reorders the three values into triplets so that you can do what you wish with them (in this case, display them).

n = 10;
arrivals =Round[RandomVariate[SkewNormalDistribution[30, 5, -4], n]];
departures = Round[RandomVariate[SkewNormalDistribution[2, 1, 0], n]];
remaining = Clip[Accumulate[arrivals - departures], {0, Infinity}];
Transpose[{arrivals, departures, remaining}]

Sample output is

{{26, 2, 24}, {27, 3, 48}, {26, 0, 74}, {22, 3, 93}, {27, 1, 119}, 
 {24, 3, 140}, {29, 1, 168}, {26, 1, 193}, {23, 1, 215}, {31, 3, 243}}

which shows that with these parameters, your queue is pretty unstable.

  • $\begingroup$ Seems to work as a animation too: Manipulate[ arrivals = Round[RandomVariate[SkewNormalDistribution[30, 5, -4], n]]; departures = Round[RandomVariate[SkewNormalDistribution[2, 1, 0], n]]; remaining = Clip[Accumulate[arrivals - departures], {0, Infinity}]; data = Transpose[{arrivals, departures, remaining}]; ListAnimate[BarChart[#, ChartLabels -> {"Arrivals", "Departures", "Remaining"},ImagePadding -> 5, PlotRange -> {0, 300}] & /@ data], {n, 1, 30, 1}] $\endgroup$
    – cormullion
    Jun 23, 2013 at 7:25
  • $\begingroup$ @bill s: Works great, and Clip definitely looks much better than the if statement. Thank you. $\endgroup$ Jun 23, 2013 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.