0

I'm having trouble with this assignment that requires me to create 50 random unique numbers without using ArrayLists. I'm required to use a boolean array that checks whether the random number has already been generated. Each number that is generated out of 50 will be set to true in the boolean array. Ex. Generating a number 23 would make check[23]=true. The problem I am having is an error in the while loop that keeps on generating a new random number even if there is no other unique number left. How can I solve this problem while still using a boolean array to check uniqueness.

int rnd;
Random rand=new Random();Random rand=new Random();
int[] nums = new int[50];
boolean[] check = new boolean[51];

rnd = rand.nextInt(50) +1;
for (int k = 0; k<50; k++)
{
    //Loop for when there number is already chosen
    while (check[rnd]==true)
    {
        rnd = rand.nextInt(50) +1;
    }
    //Sets the random unique number to a slot in the array
    if(check[rnd]==false)
    {
        nums[k]=rnd;
        check[rnd]=true;
    }
    rnd = rand.nextInt(50) +1;
}
System.out.println(nums);
9
  • 3
    So the problem you're experiencing is that you're trying to fill a 51-slot array with 50 unique integers? Commented May 27, 2014 at 19:57
  • Please be more specific about your assignment's requirements. The boolean array makes no sense. Commented May 27, 2014 at 19:57
  • possible duplicate of Generating random numbers in a range with Java Commented May 27, 2014 at 20:03
  • 3
    It looks like you're trying to generate 50 unique random integers from 1 to 50. That's really just a complicated way to say you're trying to shuffle the numbers 1-50 into a random order, isn't it? Commented May 27, 2014 at 20:04
  • 1
    Your code seems to work properly. It is generating exactly 50 unique numbers. So where's your problem? Commented May 27, 2014 at 20:17

2 Answers 2

1

Try this:

import java.util.Random;

public class random {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Random myRandom = new Random();
        int[] numbers = new int[50];
        boolean[] check = new boolean[50];
        int amountFilled = 0;
        int trial;
        while (amountFilled < 50) {
            trial = myRandom.nextInt(50);
            if (!check[trial]) {
                check[trial] = true;
                numbers[amountFilled] = trial;
                amountFilled++;
            }
        }
        for (int i = 0; i < 50; i++) {
            System.out.println(numbers[i]);
        }
    }
}

Your real problem was the System.out.println(nums); statement. It doesn't do what you what it to do. And the double Random rand=new Random();. The rest of the code is OK. I rewrote it in a clearer/simpler way, but what you had already works if you fix the output statement.

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

Comments

0

Few tweaks done:

public class Test {

    public static void main(String args[]){
        int rnd;

        Random rand=new Random();
        int[] nums = new int[50];
        boolean[] check = new boolean[50];
        for (int k = 0; k<50; k++)
        {
            rnd = rand.nextInt(50);

            //Loop for when there number is already chosen
            while (check[rnd])
            {
                rnd = rand.nextInt(50);

            }
//Sets the random unique number to a slot in the array
                nums[k]=rnd;
                check[rnd]=true;

        }

        for(int num : nums){
            System.out.println("\n" + num);

        }

    }
}

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.