Reputation: 254
I'm trying to write a program that generates a random school timetable with random positions and a random amount of hours for each teacher, but with a fixed total amount of time per day. For now the program is written to work with two days, and I'm encountering an issue: the random-generated values for the amount of time between the two days are the same:
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);
}
}
specifically the value of x1 is the same of x2, the one of y1 is the same of y2 and so on.
Upvotes: 1
Views: 566
Reputation: 10003
Looks like you are using the same seed. see:
http://docs.oracle.com/javase/6/docs/api/java/util/Random.html
Upvotes: 1
Reputation:
LOL, it's a copy-paste error
last line should read
System.out.println("x2 = " +x2 +newLine +"y2 = " +y2 +newLine +"z2 = " +z2 +newLine +"m2 = " +m2 +newLine);
but it sounds like a classic random-seed problem. That's pretty funny.
Upvotes: 0
Reputation: 86381
You're Random construction is fine - you're using the default constructor, which automatically uses the time as a seed:
public Random() { this(System.currentTimeMillis()); }
But you have a copy/paste error in your last debug-print statement. You're label says x2, but you're printing x1, etc.
System.out.println("x2 = " +x1 +newLine +"y2 = " +y1 +newLine +"z2 = " +z1 +newLine +"m2 = " +m1 +newLine);
Upvotes: 3
Reputation: 3029
I cannot see any initialization of the pseudorandom number generator.
You need to set the seed of the PRNG.
Upvotes: 1