0

I am trying to write a program that selects 5 integers at random between 1 and 6. I then need the program to display the missing integer. I can't figure out how to have it not display "0" as an integer. This is what I have so far...

import java.util.Random;

public class Task6
{
    public static void main(String[] args)
    {
        int[] numbers = new int[5];
        Random random = new Random();

        for(int n = 1; n < 5; n++)
        { 
            int select = random.nextInt(n + 1);     //shuffle generator so it will not duplicate numbers      
            numbers[n] = numbers[select];
            numbers[select] = n;
        }//end for statement
        for(int number : numbers)
        {
            System.out.println("Numbers selected : " + number);
        }//end for

    }
}

I have to have a O(n^2) operation in this as well.

Joseph Kraemer
  • 111
  • 1
  • 2
  • 10

4 Answers4

2

If I understand correctly, you want your random numbers to only be between 1 and 6 (inclusive)? If that's the case then you need to restrict the range of what the RNG can actually spit out, using code similar to this:

   /**
    * Returns a pseudo-random number between min and max, inclusive.
    * The difference between min and max can be at most
    * <code>Integer.MAX_VALUE - 1</code>.
    *
    * @param min Minimum value
    * @param max Maximum value.  Must be greater than min.
    * @return Integer between min and max, inclusive.
    * @see java.util.Random#nextInt(int)
    */
    public static int randInt(int min, int max) {

        // NOTE: Usually this should be a field rather than a method
        // variable so that it is not re-seeded every call.
        Random rand = new Random();

        // nextInt is normally exclusive of the top value,
        // so add 1 to make it inclusive
        int randomNum = rand.nextInt((max - min) + 1) + min;

        return randomNum;
    }

Also, your for loop:

for(int n = 1; n < 5; n++) { ... }

will not generate 5 numbers, it will generate 4. Think about the constraints of the loop; it will run through once with n = 1, 2, 3, 4, and then stop (5 is not less than 5).

If you want 5 iterations, you can do this:

for (int n = 0; n < 5; n++) { ... }.

or this:

for (int n = 1; n <= 5; n++) { ... }

Your number of iterations and your random number range don't need to be related.

Check this excellent answer for more detail if you need it.

Community
  • 1
  • 1
gcgrant
  • 351
  • 4
  • 12
1

Create a method that applies your constraints to the random number. Here is an example.

// this assumes that only 0 is unacceptable.
private static int myRandomBlammy()
{
    int returnValue;

    do
    {
        returnValue = blam; // replace blam with some way of generating a random integer.
    } while (returnValue == 0);

    return returnValue;
}
kgh
  • 157
  • 11
DwB
  • 33,855
  • 10
  • 50
  • 78
0

You should have an if statement on your loop that goes through the numbers.

for(int number : numbers)
{
    if(number != 0){
        System.out.println("Numbers selected : " + number);
    }
}//end for
CuBonso
  • 345
  • 1
  • 3
  • 11
0

I am trying to write a program that selects 5 integers at random between 1 and 6. I then need the program to display the missing integer. I can't figure out how to have it not display "0" as an integer.

From your question, there may be a case where your 5 random integers will not all be unique, and there may be more than one unique number that was not generated.

I would handle this with an array that counts how many of each number is generated:

import java.util.Random;

public class RandomCounter
{
    /**
     * An example that uses array indices to count how many random 
     * numbers are generated in a range.
     */
    public static void main(String[] args)
    {
        //use an array of size n + 1 (ignore the zero index)
        int[] numbers = new int[7];
        Random r = new Random();

        //generate random numbers
        for (int i = 1; i < 6; i++){
            int next = r.nextInt(6) + 1;
            numbers[next]++;  //count each number at the index
        }

        //print any numbers that didn't occur at least once.
        for(int i = 1; i < numbers.length; i++){
            if(numbers[i] != 0){
                System.out.println(i);
            }   
        }
    }
}

Replace the last for-loop with this code snippet to see how many of each number occurred:

//print how many of each number occurred.
for(int i = 1; i < numbers.length; i++){
    System.out.println (i + ": " + numbers[i]);         }
}

Array index counting is a useful way to dynamically count occurrences of numbers.

aaroncarsonart
  • 832
  • 7
  • 23