1

While I was trying to create a small GUI in Java, I've stumbled onto this small issue with arrays.

I've tried inserting Random Integers into an one dimensional array, only to find out that the Random Integers won't get assigned.

//Declaring an Integer Array    
int[] wuerfel = new int[2];

//It will loop once while assigning a random number to the array
            for(int i = 0;  i <= 1; i++) {
                Random rand = new Random(6);
                int zahlen = rand.nextInt(6) + 1;
                wuerfel[i] = zahlen;
            }

System.out.println(Arrays.toString(wuerfel));

I expect the output from the array to be a number between 1 - 6.

However, I keep receiving [2,2] as a result every time I try to rerun.

3
  • 3
    you set up a seed for your Random and you create new Random every loop iteration so it just returns same number every time. Commented May 2, 2019 at 10:45
  • You can consider using Math.random() for getting random numbers Commented May 2, 2019 at 10:49
  • After finding out that I had set a seed, I rewrote my code and moved the Random class object out of the loop, this time I have added "Math.random()" and it works flawlessly. Thank you all for your help! Commented May 2, 2019 at 12:43

3 Answers 3

2

The constructor call new Random(6) doesn't do what you think it does: 6 is the seed, rather than the range of possible outputs. Therefore it will produce the same output every time.

Possible solutions:

  • Use the no-argument constructor for Random() instead, which will give it a different seed each time.
  • Declare and initialise rand outside the loop, with or without an explicit seed.
  • Use Math.random().
Sign up to request clarification or add additional context in comments.

Comments

2

So in your code you set up a seed for your Random and you create new Random object every loop iteration so it just returns same number every time. If you use seed the documentation of Random class says :

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers.

In your case you can get rid of seed value from your constructor or move Random class object creation outside of the loop :

public static void main(String[] args) {
        int[] arr = new int[2];

        Random rand = new Random();

        for(int i = 0;  i <= 1; i++) {
            int zahlen = rand.nextInt(6) + 1;
            arr[i] = zahlen;
        }

        System.out.println(Arrays.toString(arr));
    }

Here I moved creation of Random instance outside of the loop so only one object is created and I am not passing seed to the constructor. I could pass the seed but in this case it is not needed as I don't need to create more instances of Random and I dont need them to generate same results.

Comments

-1
*  
    int[] wuerfel = new int[2];
    //It will loop once while assigning a random number to the array
           Random rand = new Random();                 
               for(int i = 0;  i <= 1; i++) {
                    int zahlen = rand.nextInt(6) + 1;
                    wuerfel[i] = zahlen;
                }
    System.out.println(Arrays.toString(wuerfel));

* try this one create Random object with passing parameter

1 Comment

The creation of the Random object should be moved out of the loop as well.

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.