From Flat Random Numbers to Arrival Times

Random VariablesMedium+20 XP+20

From Flat Random Numbers to Arrival Times

A help-desk simulator needs realistic times at which customers arrive. The gap XXX, in minutes, between one arrival and the next is a continuous random variable with CDF

F(x)=1−e−x/μ,x≥0F(x) = 1 - e^{-x/\mu}, \qquad x \ge 0F(x)=1−e−x/μ,x≥0

where μ\muμ (mean_gap) is the average gap in minutes.

Two tools you will need:

  • e≈2.71828e \approx 2.71828e≈2.71828 is a fixed number, like π\piπ. eae^{a}ea means eee raised to the power aaa; in Python it is math.exp(a).
  • The natural logarithm ln⁡\lnln undoes it: for any number aaa, ln⁡(ea)=a\ln(e^{a}) = aln(ea)=a. So if ea=ce^{a} = cea=c for a positive number ccc, then a=ln⁡(c)a = \ln(c)a=ln(c). In Python, ln⁡(c)\ln(c)ln(c) is math.log(c).

The simulator's random-number generator can only produce uniform draws: numbers uuu between 0 and 1, with every value equally likely. To turn a draw into a gap, it reads the CDF in reverse. The gap for draw uuu is the value xxx at which the CDF reaches uuu:

F(x)=uF(x) = uF(x)=u

This works because FFF climbs from 0 towards 1 and never goes back down, so each uuu picks out exactly one xxx. Where FFF climbs steeply (gaps that are likely), a wide band of uuu values all land on a short stretch of xxx, so many draws give gaps there. Where FFF climbs slowly (gaps that are rare), a thin band of uuu values is spread over a long stretch of xxx, so few draws give gaps there. The gaps produced this way have exactly the CDF FFF.

The first customer arrives one gap after time 0, and each later customer arrives one gap after the customer before.

Task: write arrival_times(uniforms, mean_gap) that turns each draw, in order, into a gap and returns the list of arrival times (minutes since time 0), each rounded to 4 decimal places.

  • Every draw satisfies 0≤u<10 \le u < 10≤u<1, and mean_gap is positive.
  • Use the draws exactly as given: the kkk-th draw produces the kkk-th gap.
  • Round only the arrival times you return, not the gaps along the way.
  • An empty list of draws means no customers, so return [].

Examples

uniforms = [0.5, 0.2, 0.9]
mean_gap = 2.0
returns [1.3863, 1.8326, 6.4378]
uniforms = [0.1, 0.6, 0.3, 0.75]
mean_gap = 1.0
returns [0.1054, 1.0217, 1.3783, 2.7646]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run