Java ????????????
???????????? ???????[ 2012/8/2 10:36:39 ] ????????
public class RandomTest{
static Random random = new Random();
static int r1(int n) {
return (int)(Math.random() * n);
}
static int r2(int n) {
return random.nextInt(n);
}
public static void main(String[] args) {
long t1 = System.nanoTime();
for (int i=0;i<10000;i++) {
r1(1024);
}
long t2 = System.nanoTime();
System.out.println(t2 - t1);
t1 = System.nanoTime();
for (int i=0;i<10000;i++) {
r2(1024);
}
t2 = System.nanoTime();
System.out.println(t2 - t1);
}
}
?????????
????3422502
????1335365
???????????????????Ч???nextInt(n)????Math.random()??á?
?????????????
????Random???next(n)?????????????seed??????????nextseed?????seedλ48λ??bit????????????????????????????????????????
protected int next(int bits) {
long oldseed?? nextseed;
AtomicLong seed = this.seed;
do {
oldseed = seed.get();
nextseed = (oldseed * multiplier + addend) & mask;
} while (!seed.compareAndSet(oldseed?? nextseed));
return (int)(nextseed >>> (48 - bits));
}
??????
![](/images/ad-banner/ad-banner.png)
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11