Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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];

Do[
 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}
]
share|improve this question
2  
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. :) –  Oleksandr R. Jun 23 '13 at 0:52
    
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. –  user1002327 Jun 23 '13 at 0:55
    
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. –  Oleksandr R. Jun 23 '13 at 0:58
    
Thank you for the information on scoping. I've updated my answer with the code. –  user1002327 Jun 23 '13 at 1:07

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
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}] –  cormullion Jun 23 '13 at 7:25
    
@bill s: Works great, and Clip definitely looks much better than the if statement. Thank you. –  user1002327 Jun 23 '13 at 15:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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