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