View Javadoc
1   package org.opentrafficsim.road.gtu.generator;
2   
3   import org.djunits.value.vdouble.scalar.Speed;
4   import org.opentrafficsim.core.distributions.Generator;
5   import org.opentrafficsim.core.distributions.ProbabilityException;
6   import org.opentrafficsim.core.gtu.behavioralcharacteristics.ParameterException;
7   
8   import nl.tudelft.simulation.jstats.streams.StreamInterface;
9   
10  /**
11   * <p>
12   * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
14   * <p>
15   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 28 nov. 2016 <br>
16   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
17   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
18   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
19   */
20  
21  public class SpeedGenerator implements Generator<Speed>
22  {
23  
24      /** Min speed. */
25      private final Speed minSpeed;
26  
27      /** Difference between max and min speed. */
28      private final Speed deltaSpeed;
29  
30      /** Random stream. */
31      private final StreamInterface stream;
32  
33      /**
34       * @param minSpeed min speed
35       * @param maxSpeed max speed
36       * @param stream random stream
37       */
38      public SpeedGenerator(final Speed minSpeed, final Speed maxSpeed, final StreamInterface stream)
39      {
40          this.minSpeed = minSpeed;
41          this.deltaSpeed = maxSpeed.minus(minSpeed);
42          this.stream = stream;
43      }
44  
45      /** {@inheritDoc} */
46      @Override
47      public Speed draw() throws ProbabilityException, ParameterException
48      {
49          return this.minSpeed.plus(this.deltaSpeed.multiplyBy(this.stream.nextDouble()));
50      }
51  
52  }