public class XORShiftRandom {private long last;private long inc;public XORShiftRandom() { this(System.currentTimeMillis());}public XORShiftRandom(long seed) { this.last = seed | 1; inc = seed;}public int nextInt(int max) { last ^= (last << 21); last ^= (last >>> 35); last ^= (last << 4); inc += 123456789123456789L; int out = (int) ((last+inc) % max); return (out < 0) ? -out : out;}}
我做了一个简单的测试,它是大约 四 倍的速度作为
java.util.Random
如果您对它的工作方式很感兴趣,可以阅读以下文章:
免责声明:
上面的代码仅用于研究目的,不能代替库存Random或SecureRandom。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)