View Javadoc
1   package org.opentrafficsim.road.gtu.generator.headway;
2   
3   import java.util.function.Supplier;
4   
5   import org.djunits.value.vdouble.scalar.Duration;
6   import org.djunits.value.vdouble.scalar.Frequency;
7   
8   import nl.tudelft.simulation.jstats.streams.StreamInterface;
9   
10  /**
11   * Headway supplier using independent arrivals (exponential distribution) at a fixed average rate.
12   * <p>
13   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
14   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
15   * </p>
16   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
17   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
18   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
19   */
20  public class HeadwayGenerator implements Supplier<Duration>
21  {
22  
23      /** Demand level. */
24      private final Frequency demand;
25  
26      /** the stream information. */
27      private final StreamInterface stream;
28  
29      /**
30       * Constructor.
31       * @param demand demand.
32       * @param stream the stream to use for generation.
33       */
34      public HeadwayGenerator(final Frequency demand, final StreamInterface stream)
35      {
36          this.demand = demand;
37          this.stream = stream;
38      }
39  
40      @Override
41      public Duration get()
42      {
43          return Duration.ofSI(-Math.log(this.stream.nextDouble()) / this.demand.si);
44      }
45  
46  }