动手动脑

纯随机数算法
public class RandomGenerator {
private static final long MODULUS = (1L << 31) - 1; // 2^31 - 1
private static final long MULTIPLIER = 75;
private static final long C = 0;
private long seed;

public RandomGenerator(long seed) {
this.seed = seed;
}

public int nextRandom() {
seed = (MULTIPLIER * seed + C) % MODULUS;
return (int) seed;
}

public int[] generateRandomNumbers(int count) {
int[] randomNumbers = new int[count];
for (int i = 0; i < count; i++) {
randomNumbers[i] = nextRandom();
}
return randomNumbers;
}

public static void main(String[] args) {
long seed = System.currentTimeMillis(); // 使用当前时间作为种子
RandomGenerator generator = new RandomGenerator(seed);
int[] randomNumbers = generator.generateRandomNumbers(1000);

for (int i = 0; i < randomNumbers.length; i++) {
System.out.println(randomNumbers[i]);
}
}
}
posted @ 2023-09-20 19:35  赵千万  阅读(5)  评论(0编辑  收藏  举报