1 29 30 package com.caucho.util; 31 32 import java.security.SecureRandom ; 33 import java.util.Random ; 34 35 38 public class RandomUtil { 39 static private long _seed = System.currentTimeMillis(); 40 static private Random _random; 41 static private boolean _isTest; 42 43 46 public synchronized static long getRandomLong() 47 { 48 return getRandom().nextLong(); 49 } 50 51 54 public synchronized static int nextInt(int n) 55 { 56 return getRandom().nextInt(n); 57 } 58 59 62 public static void addRandom(String random) 63 { 64 if (random == null) 65 return; 66 67 for (int i = 0; i < random.length(); i++) 68 addRandom(random.charAt(i)); 69 } 70 71 74 public static void addRandom(long seed) 75 { 76 Random random = getRandom(); 77 if (random instanceof SecureRandom ) 78 ((SecureRandom ) random).setSeed(seed); 79 } 80 81 84 private static Random getRandom() 85 { 86 if (_random == null) { 87 _random = new SecureRandom (); 88 _random.setSeed(_seed); 89 } 90 91 return _random; 92 } 93 94 97 public static void setTestSeed(long seed) 98 { 99 _seed = seed; 100 _isTest = true; 101 _random = new Random (seed); 102 } 103 } 104 | Popular Tags |