用Java编写线性同余器生成10以内正整数不重复的2个随机数字,不使用random的api,每次运行的数字都不相同
时间: 2024-06-03 22:06:54 浏览: 155
Java实现生成n个不重复的随机数
public class LinearCongruentialGenerator {
private static final int a = 3;
private static final int c = 7;
private static final int m = 10;
private int seed;
public LinearCongruentialGenerator(int seed) {
this.seed = seed;
}
public int nextInt() {
seed = (a * seed + c) % m;
return seed;
}
public static void main(String[] args) {
LinearCongruentialGenerator generator = new LinearCongruentialGenerator(1);
int num1 = generator.nextInt();
int num2 = generator.nextInt();
while (num1 == num2) {
num2 = generator.nextInt();
}
System.out.println(num1);
System.out.println(num2);
}
}
阅读全文