Java 每次都得到相同的随机数
我正试图编写一个程序,生成一个随机的学校时间表,每个老师的位置和时间都是随机的,但每天的总时间是固定的。目前,该程序的编写时间为两天,我遇到了一个问题:两天之间随机生成的时间值是相同的:Java 每次都得到相同的随机数,java,random,Java,Random,我正试图编写一个程序,生成一个随机的学校时间表,每个老师的位置和时间都是随机的,但每天的总时间是固定的。目前,该程序的编写时间为两天,我遇到了一个问题:两天之间随机生成的时间值是相同的: import java.util.Random; public class randomTimetable { public static void main(String[] args) { String newLine = System.getProperty("lin
import java.util.Random;
public class randomTimetable {
public static void main(String[] args) {
String newLine = System.getProperty("line.separator");
System.out.println("For each day (x + y + ... n) >= 5 and" +newLine +"(x && y && ... n) <= 2" +newLine);
createTimetable();
}
private static void createTimetable() {
String x_g1 = "x";
String y_g1 = "y";
String z_g1 = "z";
String m_g1 = "m";
String[] arrayTimetablePosition1={x_g1, y_g1, z_g1, m_g1};
String newLine = System.getProperty("line.separator");
System.out.println("Work In Progress" +newLine +"Total subjects = 5" +newLine +"Day 1");
Random rand = new Random();
int min = 0;
int max = 2;
int x1 = rand.nextInt(max - min + 1) + min;
int y1 = rand.nextInt(max - min + 1) + min;
int z1 = rand.nextInt(max - min + 1) + min;
int m1 = rand.nextInt(max - min + 1) + min;
while((x1 + y1 + z1 + m1) != 5) {
x1 = rand.nextInt(max - min + 1) + min;
y1 = rand.nextInt(max - min + 1) + min;
z1 = rand.nextInt(max - min + 1) + min;
m1 = rand.nextInt(max - min + 1) + min;
}
System.out.println("x1 = " +x1 +newLine +"y1 = " +y1 +newLine +"z1 = " +z1 +newLine +"m1 = " +m1 +newLine);
System.out.println("Total subjects = 5" +newLine +"Day 2");
int x2 = rand.nextInt(max - min + 1) + min;
int y2 = rand.nextInt(max - min + 1) + min;
int z2 = rand.nextInt(max - min + 1) + min;
int m2 = rand.nextInt(max - min + 1) + min;
while((x2 + y2 + z2 + m2) != 5 && (x1 == x2 || y1 == y2 || z1 == z2 || m1 == m2)) {
x2 = rand.nextInt(max - min + 1) + min;
y2 = rand.nextInt(max - min + 1) + min;
z2 = rand.nextInt(max - min + 1) + min;
m2 = rand.nextInt(max - min + 1) + min;
}
System.out.println("x2 = " +x1 +newLine +"y2 = " +y1 +newLine +"z2 = " +z1 +newLine +"m2 = " +m1 +newLine);
}
}
import java.util.Random;
公开课{
公共静态void main(字符串[]args){
字符串newLine=System.getProperty(“line.separator”);
System.out.println(“对于每天(x+y+…n)>=5和“+newLine+”(x&&y&&…n)看起来您使用的是相同的种子。请参阅:
看起来您使用的是相同的种子。请参阅:
我看不到伪随机数生成器的任何初始化
您需要设置PRNG的种子。我看不到伪随机数生成器的任何初始化
您需要设置PRNG的种子。您的随机构造很好-您使用的是默认构造函数,它会自动将时间用作种子:
public Random() { this(System.currentTimeMillis()); }
但在上一个调试打印语句中有一个复制/粘贴错误。标签上写的是x2,但打印的是x1,等等
System.out.println("x2 = " +x1 +newLine +"y2 = " +y1 +newLine +"z2 = " +z1 +newLine +"m2 = " +m1 +newLine);
您的随机构造很好-您使用的是默认构造函数,它会自动使用时间作为种子:
public Random() { this(System.currentTimeMillis()); }
但在上一个调试打印语句中有一个复制/粘贴错误。标签上写的是x2,但打印的是x1,等等
System.out.println("x2 = " +x1 +newLine +"y2 = " +y1 +newLine +"z2 = " +z1 +newLine +"m2 = " +m1 +newLine);
哈哈,这是复制粘贴错误
最后一行应该是
System.out.println(“x2=“++x2+换行符+”y2=“++y2+换行符+”z2=“++z2+换行符+”m2=“+m2+换行符”)
但这听起来像是一个典型的随机种子问题。这很有趣。哈哈,这是一个复制粘贴错误
最后一行应该是
System.out.println(“x2=“++x2+换行符+”y2=“++y2+换行符+”z2=“++z2+换行符+”m2=“+m2+换行符”)
但这听起来像是一个经典的随机种子问题。这很有趣。相反,这个问题使用一个接受种子的随机构造函数,每次传递的种子都是相同的。它不是重复的。另一个是C#,这是Java。这里的问题不是种子问题。相反,这个问题使用的是随机构造函数它接受一个种子,并且每次都传递相同的种子。它不是重复的。另一个是C#,这是Java。这里的种子不是问题。谢谢。阅读代码是值得的。从标题开始,我猜这将是使用相同初始种子的另一种情况。谢谢。当您太累而无法检查代码时,会发生这种情况.:)谢谢。阅读代码是值得的。从标题开始,我猜这将是使用相同初始种子的另一种情况。谢谢。当你太累而无法检查代码时就会发生这种情况。:)