动手动脑
纯随机数算法
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]);
}
}
}
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]);
}
}
}
本文来自博客园,作者:赵千万,转载请注明原文链接:https://www.cnblogs.com/zhaoqianwan/p/17718201.html
千万千万赵千万