1 10 package org.mmbase.util; 11 12 import java.util.Random ; 13 14 20 public class RandomPlus extends Random { 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 |