1

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.

Giovanni Berti
  • 244
  • 3
  • 12

4 Answers4

3

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);
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
1

Looks like you are using the same seed. see:

http://docs.oracle.com/javase/6/docs/api/java/util/Random.html

Pranav 웃
  • 8,469
  • 6
  • 38
  • 48
Ray Tayek
  • 9,841
  • 8
  • 50
  • 90
1

I cannot see any initialization of the pseudorandom number generator.

You need to set the seed of the PRNG.

V-X
  • 2,979
  • 18
  • 28
  • [it's a duplicate](http://stackoverflow.com/questions/10239346/new-randomx-always-generates-the-same-numbers) – V-X Mar 02 '13 at 22:23
  • On the contrary -- that question uses a Random constructor that accepts a seed, and the same seed was passed each time. – Andy Thomas Mar 02 '13 at 22:27
  • It's not a duplicate. The other is C#, this is Java. And the seeding isn't the problem here. – Don Roby Mar 02 '13 at 22:27
0

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.