1

I'm trying to create a Java program that generates an amount of seats taken in an airplane. So far, I have been able to do this, but my problem is every time I run the client the numbers generated are different. I need them to be the same every time...

I'm not sure what I'm doing wrong, can anybody help me?

import java.util.Random;
import java.util.Arrays;

public class Airplane {
    public static Random randomNumbers = new Random();
    public static int[] oSeatLeft = new int[10];
    public static int[] mSeatLeft = new int[10];
    public static int[] wSeatLeft = new int[10];
    public static int oSeat = 0;
    public static int mSeat = 0;
    public static int wSeat = 0;
    public static final int sCheck = 0;

    public void genWSeats() {

        int randSeatFill = 0;
        if (wSeat == 0) {
            for (int counter = 0; counter < wSeatLeft.length; counter++) {
                randSeatFill = randomNumbers.nextInt(2);
                if (randSeatFill == 1) {
                    wSeatLeft[counter] = 1;
                }
            }
            if (wSeat == 0) {
                wSeat++;
            }
        }
    }

    public int[] getWSeats() {
        System.out.println(java.util.Arrays.toString(wSeatLeft));
        return wSeatLeft;
    }
}

The purpose of static int wSeat is supposed to be a checker. If wSeat is greater than zero, then it should not randomly generate numbers for the array. Not sure exactly what is going wrong here....

1
  • 4
    I'm creating random data and its coming out randomly. What sort of magic is this? Commented Apr 24, 2013 at 4:56

3 Answers 3

2

Use the Random constructor with seed

public static Random randomNumbers = new Random(42);

This way always the same sequence of random numbers is generated. 42 is only an example, you can use any value you want.

Sign up to request clarification or add additional context in comments.

3 Comments

okay great, seems to work so far. I just deleted the checker if statement, and the answers stay the same.... I assume this is okay...
Thanks! That helps a wicked lot! So my problem now is.... when I create multiple objects that are an extension of this class, the random numbers in each class are still the same.... is there a way to only initialize one variable, but have it generate different numbers for each class?
@Brian Murphy: I think this could be a little tricky. Each class would need its own static Random-instance.
1

Pass a seed on initialization with Random(long seed). This will guarantee that the sequence of numbers generated is always the same (since it's a pseudo-random number generator).

Comments

1

Pass Seed in constructor of Random it will generate the same number every time

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.