KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > RandomPool


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10
11 package org.mmbase.util;
12
13 /**
14  * Class for holding a pool of random numbers.
15  * Calling {@link #stir} takes care of generating a new 'random' number.
16  * Calling {@link #value} returns the current random number.
17  *
18  * @author Rico Jansen
19  * @author Pierre van Rooden (javadocs)
20  * @version $Id: RandomPool.java,v 1.8 2005/10/05 10:44:00 michiel Exp $
21  */

22 public class RandomPool {
23
24     /**
25      * Current random value.
26      * Initial to a value from MD5 64-bit pool of randomness.
27      * This values is maintained by {@link #stir}, and is
28      * the value that is actually returned by {@link #value}
29      * #see {@link #stir}
30      */

31     private static long ranPool = 0x67452301efcdab89L;
32
33     /**
34      * Pool of magic numbers.
35      * The following 31 constants are the first 62 "magic" numbers
36      * from the MD5 algorithm, RFC1321, concatenated in pairs to
37      * form 64-bit Java long words. Any random 64-bit values would do,
38      * but these were selected from a public source for added user confidence.
39      * The stir algorithm itself has nothing to do with MD5.
40      */

41      // No need to do it everytime EJJ
42
private static long p[] = {
43         0xd76aa478e8c7b756L, // 1,2
44
0x242070dbc1bdceeeL, // 3,4
45
0xf57c0faf4787c62aL, // 5,6
46
0xa8304613fd469501L, // 7,8
47
0x698098d88b44f7afL, // 8,10
48
0xffff5bb1895cd7beL, // 11,12
49
0x6b901122fd987193L, // 13,14
50
0xa679438e49b40821L, // 15,16
51
0xf61e2562c040b340L, // 17,18
52
0x265e5a51e9b6c7aaL, // 19,20
53
0xd62f105d02441453L, // 21,22
54
0xd8a1e681e7d3fbc8L, // 23,24
55
0x21e1cde6c33707d6L, // 25,26
56
0xf4d50d87455a14edL, // 27,28
57
0xa9e3e905fcefa3f8L, // 29,30
58
0x676f02d98d2a4c8aL, // 31,32
59
0xfffa39428771f681L, // 33,34
60
0x6d9d6122fde5380cL, // 35,36
61
0xa4beea444bdecfa9L, // 37,38
62
0xf6bb4b60bebfbc70L, // 39,40
63
0x289b7ec6eaa127faL, // 41,42
64
0xd4ef308504881d05L, // 43,44
65
0xd9d4d039e6db99e5L, // 45,46
66
0x1fa27cf8c4ac5665L, // 47,48
67
0xf4292244432aff97L, // 49,50
68
0xab9423a7fc93a039L, // 51,52
69
0x655b59c38f0ccc92L, // 53,54
70
0xffeff47d85845dd1L, // 55,56
71
0x6fa87e4ffe2ce6e0L, // 57,58
72
0xa30143144e0811a1L, // 59,60
73
0xf7537e82bd3af235L // 61,62
74
};
75
76     /**
77      * Create a pool.
78      * Use the current time (long milliseconds) as a source of randomness
79      */

80     public RandomPool() {
81         stir(System.currentTimeMillis());
82     }
83
84     /**
85      * Create a pool.
86      * @param init the source of randomness
87      */

88     public RandomPool(long init) {
89         stir(init);
90     }
91
92     /**
93      * Maintain a pool of randomness using a modified 64-bit
94      * congruential generator with multipliers dynamically
95      * selected from a set of pseudo-random values.
96      * @param x the source of randomness
97      */

98     public synchronized void stir(long x) {
99         int pIndex;
100
101         pIndex = mod(ranPool, p.length);
102         ranPool = (ranPool + x)*p[pIndex];
103         pIndex = mod(ranPool, p.length);
104         ranPool = ranPool ^ p[pIndex];
105     }
106
107     /**
108      * Returns the current random value.
109      */

110     public long value() {
111         return(ranPool);
112     }
113
114     /**
115      * Stirs, then returns the (new) current random value.
116      * Use the current time (long milliseconds) as a source of randomness
117      * for stirring.
118      */

119     public long value_and_stir() {
120         stir(System.currentTimeMillis());
121         return(ranPool);
122     }
123
124     /**
125      * Stirs, then returns the (new) current random value.
126      * @param mix the source of randomness for stirring
127      */

128     public long value_and_stir(long mix) {
129         stir(mix);
130         return(ranPool);
131     }
132
133     /**
134      * Returns the positive modulus of the arguments.
135      * @param x the argument to modulate
136      * @param y the argument to modulate with
137      * @return the positive modulus of x modulus y.
138      */

139     private int mod (long x, long y) {
140         if (x<0) x=-x;
141         if (y<0) y=-y;
142         return (int) (x % y);
143     }
144 }
145
146
Popular Tags