Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #1 from mquinson/master
[simgrid.git] / contrib / psg / src / peersim / util / ExtendedRandom.java
1 /*
2  * Copyright (c) 2003-2005 The BISON Project
3  *
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.
7  *
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.
12  *
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.
16  *
17  */
18
19 package peersim.util;
20
21 import java.util.Random;
22 import java.lang.Math;
23
24 /**
25  * Extends the functionality of <code>java.util.Random</code>.
26  */
27 public class ExtendedRandom extends Random {
28
29 private long lastSeed;
30
31 // -------------------------------------------------------------------------
32
33 /** Calls super constructor. Also stores the seed to be returned by
34 {@link #getLastSeed}. */
35 public ExtendedRandom(long seed) {
36         
37         super(seed);
38         lastSeed = seed;
39 }
40
41 // -------------------------------------------------------------------------
42
43 /**
44  * Extracts the next integer, according to a Poisson distribution.
45  * 
46  * @param mean The mean of the Poisson distribution.
47  * @return An integer Poisson extraction.
48  */
49 public int nextPoisson(double mean) {
50         
51         double emean = Math.exp(-1 * mean);
52         double product = 1;
53         int count = 0;
54         int result = 0;
55         while (product >= emean) {
56                 product *= nextDouble();
57                 result = count;
58                 count++; // keep result one behind
59         }
60         return result;
61 }
62
63 // -------------------------------------------------------------------------
64
65 /**
66 * Implements nextLong(long) the same way nexInt(int) is implemented in
67 * java.util.Random.
68 * @param n the bound on the random number to be returned. Must be positive.
69 * @return a pseudorandom, uniformly distributed long value between 0
70 * (inclusive) and n (exclusive).
71 */
72 public long nextLong(long n) {
73
74         if (n<=0)
75                 throw new IllegalArgumentException("n must be positive");
76         
77         if ((n & -n) == n)  // i.e., n is a power of 2
78         {       
79                 return nextLong()&(n-1);
80         }
81         
82         long bits, val;
83         do
84         {
85                 bits = (nextLong()>>>1);
86                 val = bits % n;
87         }
88         while(bits - val + (n-1) < 0);
89         
90         return val;
91 }
92
93 // -------------------------------------------------------------------------
94
95 /** Sets random seed. Calls super method but also stores the seed to be
96 returned by {@link #getLastSeed}. */
97 public void setSeed( long seed ) {
98         
99         super.setSeed(seed);
100         lastSeed = seed;
101 }
102
103 // -------------------------------------------------------------------------
104
105 /**
106 * Returns the last random seed that was set explicitly. Either at
107 * construction time or through {@link #setSeed}.
108 */
109 public long getLastSeed() { return lastSeed; }
110
111 // -------------------------------------------------------------------------
112
113 /*
114 public static void main(String[] args) {
115
116         ExtendedRandom er = new ExtendedRandom(12345678);
117         for(int i=0; i<100; ++i)
118                 System.out.println(er.nextLong(Long.parseLong(args[0])));
119         
120 }
121 */
122 }
123