KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.mmbase.util;
11
12 import java.util.Random JavaDoc;
13
14 /**
15  * Better random function (see Knuth)
16  * @application SCAN
17  * @author Rico Jansen
18  * @version $Id: RandomPlus.java,v 1.6 2004/09/30 14:07:13 pierre Exp $
19  */

20 public class RandomPlus extends Random JavaDoc {
21     private int table[];
22     private int tablesize;
23
24     public RandomPlus() {
25         this(System.currentTimeMillis(),97);
26     }
27
28     public RandomPlus(long seed) {
29         this(seed,97);
30     }
31
32     public RandomPlus(long seed,int size) {
33         super(seed);
34         tablesize=size;
35         table=new int[tablesize];
36         for (int i=0;i<tablesize;i++) {
37             table[i]=super.nextInt();
38         }
39     }
40
41     public void setSeed(long seed) {
42         super.setSeed(seed);
43         for (int i=0;i<tablesize;i++) {
44             table[i]=super.nextInt();
45         }
46     }
47
48     private synchronized int mynext(int bits) {
49         int idx;
50         int rtn;
51
52         idx=(int)((Math.abs(super.nextInt()))%tablesize);
53         rtn=table[idx];
54         table[idx]=super.nextInt();
55         return rtn>>>(32-bits);
56     }
57
58     public int nextInt() {
59         return mynext(32);
60     }
61
62     public long nextLong() {
63         return mynext(32)<<32L + mynext(32);
64     }
65
66     public float nextFloat() {
67         int i=mynext(30);
68         return i /((float)(1<<30));
69     }
70
71     public double nextDouble() {
72         long l = ((long)(mynext(27))<<27) + mynext(27);
73         return l / (double)(1L<<54);
74     }
75 }
76
Popular Tags