2 * Copyright (c) 2003-2005 The BISON Project
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 package peersim.vector;
21 import peersim.config.*;
22 import peersim.core.*;
23 import peersim.dynamics.*;
26 * Initializes the values drawing uniform random samples from the range
27 * [{@value #PAR_MIN}, {@value #PAR_MAX}[.
31 public class UniformDistribution extends VectControl implements NodeInitializer
34 //--------------------------------------------------------------------------
36 //--------------------------------------------------------------------------
39 * The upper bound of the uniform random variable, exclusive.
42 private static final String PAR_MAX = "max";
45 * The lower bound of the uniform
46 * random variable, inclusive. Defaults to -{@value #PAR_MAX}.
49 private static final String PAR_MIN = "min";
51 // --------------------------------------------------------------------------
53 // --------------------------------------------------------------------------
56 private final Number min;
59 private final Number max;
61 // --------------------------------------------------------------------------
63 // --------------------------------------------------------------------------
66 * Standard constructor that reads the configuration parameters.
67 * Invoked by the simulation engine.
68 * @param prefix the configuration prefix for this class
70 public UniformDistribution(String prefix)
74 // Read parameters based on type
75 if (setter.isInteger()) {
76 max=Long.valueOf(Configuration.getLong(prefix + "." + PAR_MAX));
77 min=Long.valueOf(Configuration.getLong(prefix + "." + PAR_MIN,
79 } else { // we know it's double or float
80 max = new Double(Configuration.getDouble(prefix+"."+PAR_MAX));
81 min = new Double(Configuration.getDouble(prefix+"."+PAR_MIN,
86 // --------------------------------------------------------------------------
88 // --------------------------------------------------------------------------
91 * Initializes the values drawing uniform random samples from the range
92 * [{@value #PAR_MIN}, {@value #PAR_MAX}[.
93 * @return always false
95 public boolean execute() {
97 if(setter.isInteger())
99 long d = max.longValue() - min.longValue();
100 for (int i = 0; i < Network.size(); ++i)
102 setter.set(i,CommonState.r.nextLong(d)+min.longValue());
107 double d = max.doubleValue() - min.doubleValue();
108 for (int i = 0; i < Network.size(); ++i)
110 setter.set(i,CommonState.r.nextDouble()*d+
118 // --------------------------------------------------------------------------
121 * Initializes the value drawing a uniform random sample from the range
122 * [{@value #PAR_MIN}, {@value #PAR_MAX}[.
123 * @param n the node to initialize
125 public void initialize(Node n) {
127 if( setter.isInteger() )
129 long d = max.longValue() - min.longValue();
130 setter.set(n,CommonState.r.nextLong(d) + min.longValue());
134 double d = max.doubleValue() - min.doubleValue();
135 setter.set(n,CommonState.r.nextDouble()*d);
139 // --------------------------------------------------------------------------