我知道種子在這裏已經有很多問題了,但我仍然在努力尋找,但如果這似乎是重複的,我很抱歉!基本上我會提示用戶輸入值(這將是種子),然後使用該種子生成隨機值。我的代碼有兩個獨立的文件,但我會盡量保持這種組織。在Java中爲隨機數生成器使用特定種子
文件1:
Random generator = new Random();
public RandomWalk(int gridSize){
gridSize = generator.nextInt();
this.gridSize = gridSize;
}
public RandomWalk(int gridSize, long seed){
generator.nextInt();
this.gridSize = gridSize;
}
文件2:
public static long seed = 0;
int i = 1;
System.out.println("Enter random seed (0 for no seed): ");
while (i == 1) {
seed = scan.nextInt();
if (!(seed == 0)) {
if (seed > 0) {
i++;
//Generate numbers based on seed entered
} else {
System.out.println("You need to enter a positive integer.");
}
} else {
System.out.println("Seed is 0, a random seed will be generated");
//Make this generate a random seed
i++;
}
}
public static void main(String[] args) {
getInput();
RandomWalk walk;
if (seed==0){
walk = new RandomWalk(gridSize);
} else {
walk = new RandomWalk(gridSize, seed);
}
walk.createWalk();
問題是什麼? – libik