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