1 package org.jgap.util; 2 3 4 27 public class randomLEcuyer extends randomX { 28 29 34 35 static final long mul1 = 40014, 36 mod1 = 2147483563, 37 mul2 = 40692, 38 mod2 = 2147483399; 39 static final int shuffleSize = 32, warmup = 19; 42 43 int gen1, gen2, state; 44 int [] shuffle; 45 46 48 50 51 public randomLEcuyer() { 52 shuffle = new int[shuffleSize]; 53 this.setSeed(System.currentTimeMillis()); 54 } 55 56 61 62 public randomLEcuyer(long seed) throws IllegalArgumentException { 63 shuffle = new int[shuffleSize]; 64 this.setSeed(seed); 65 } 66 67 69 74 75 public void setSeed(long seed) throws IllegalArgumentException { 76 int i; 77 78 if (seed == 0) { 79 throw new IllegalArgumentException ("seed must be nonzero"); 80 } 81 gen1 = gen2 = (int) (seed & 0x7FFFFFFFFL); 82 83 85 86 for (i = 0; i < warmup; i++) { 87 gen1 = (int) ((gen1 * mul1) % mod1); 88 } 89 90 92 for (i = 0; i < shuffleSize; i++) { 93 gen1 = (int) ((gen1 * mul1) % mod1); 94 shuffle[(shuffleSize - 1) - i] = gen1; 95 } 96 state = shuffle[0]; 97 } 98 99 103 104 public byte nextByte() { 105 int i; 106 107 gen1 = (int) ((gen1 * mul1) % mod1); gen2 = (int) ((gen2 * mul2) % mod2); 110 112 113 i = state / (1 + (((int) mod1) - 1) / shuffleSize); 114 115 117 state = (int) ((((long) shuffle[i]) + gen2) % mod1); 118 119 121 shuffle[i] = gen1; 122 123 return (byte) (state / (1 + (((int) mod1) - 1) / 256)); 124 } 125 }; 126 | Popular Tags |