View Javadoc

1   package net.obsearch.index.utils;
2   
3   import java.util.Date;
4   
5   import cern.jet.random.engine.MersenneTwister;
6   import cern.jet.random.engine.RandomSeedGenerator;
7   
8   /*
9    OBSearch: a distributed similarity search engine
10   This project is to similarity search what 'bit-torrent' is to downloads.
11   Copyright (C)  2007 Arnoldo Jose Muller Molina
12  
13   This program is free software: you can redistribute it and/or modify
14   it under the terms of the GNU General Public License as published by
15   the Free Software Foundation, either version 3 of the License, or
16   (at your option) any later version.
17  
18   This program is distributed in the hope that it will be useful,
19   but WITHOUT ANY WARRANTY; without even the implied warranty of
20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21   GNU General Public License for more details.
22  
23   You should have received a copy of the GNU General Public License
24   along with this program.  If not, see <http://www.gnu.org/licenses/>.
25   */
26  /**
27   * A class used to generate random numbers. We use an implementation that seems
28   * to be better than the one provided by JDK. Every time you instantiate this
29   * class, a new seed will be generated!
30   */
31  public class OBRandom {
32  
33      /**
34       * A nice random seed generator
35       */
36      private static final transient RandomSeedGenerator seedGenerator = new RandomSeedGenerator();
37  
38      /**
39       * Why to use Knuth's random generator when we can get a super-duper Mersene
40       * Twister random number generator!
41       */
42      private MersenneTwister r;
43  
44      /**
45       * When you create an OBRandom, a seed is generated by using a special seed
46       * generator.
47       */
48      public OBRandom() {
49          // r = new MersenneTwister(seedGenerator.nextSeed());
50          r = new MersenneTwister(new Date());
51      }
52  
53      /**
54       * Returns a 32 bit uniformly distributed random number in the open unit
55       * interval (0.0f,1.0f) (excluding 0.0f and 1.0f)
56       */
57      public float nextFloat() {
58          float res = r.nextFloat();
59          assert 0.0f < res &&  res < 1.0f;
60          return res;
61      }
62  
63      /**
64       * Returns the next  boolean value from this random number generator's sequence.
65       */
66      public boolean nextBoolean() {
67          return r.nextInt() >= 0;
68      }
69  
70      /**
71       * Returns a pseudorandom, uniformly distributed int value between 0
72       * (inclusive) and the specified value (exclusive)
73       * @param n
74       *                the bound on the random number to be returned. Must be
75       *                positive.
76       * @return the next pseudorandom, uniformly distributed int value between 0
77       *         (inclusive) and n (exclusive) from this random number generator's
78       *         sequence
79       */
80      public int nextInt(int n) {
81          int res = (int) (nextFloat() * (n));
82          // res--;
83          assert res >= 0 && res < n : "Result of res:" + res;
84          return res;
85      }
86  
87  }