0

I'm trying to get a simple bit of java code spit out the numbers 1-6 in random order without repeating. I've manage to get it to spit out six random integers between one and six, but it only checks to see if a number is used once. Here is the code:

import java.util.Random;

public class kirbyInt

{

    public static void main(String[] args)
    {
        int num1, num2, num3, num4, num5, num6;


        Random rand = new Random();

        num1 = rand.nextInt((6-1) + 1) + 1;
        System.out.println(num1);

        num2 = rand.nextInt((6-1) + 1) + 1;

        if (num2 == num1)
        {
            num2 = rand.nextInt((6-1) + 1) + 1;
            System.out.println(num2);
        }
        else
        {
            System.out.println(num2);
        }

        num3 = rand.nextInt((6-1) + 1) + 1;

        if (num3 == num1 || num3 == num2)
        {
            num3 = rand.nextInt((6-1) + 1) + 1;
            System.out.println(num3);
        }
        else
        {
            System.out.println(num3);
        } 

        num4 = rand.nextInt((6-1) + 1) + 1;

        if (num4 == num1 || num4 == num2 || num4 == num3)
        {
            num4 = rand.nextInt((6-1) + 1) + 1;
            System.out.println(num4);
        }
        else
        {
            System.out.println(num4);
        } 

        num5 = rand.nextInt((6-1) + 1) + 1;
        if (num5 == num1 || num5 == num2 || num5 == num3 || num5 == num4)
        {
            num5 = rand.nextInt((6-1) + 1) + 1;
            System.out.println(num5);
        }
        else
        {
            System.out.println(num5);
        } 

        num6 = rand.nextInt((6-1) + 1) + 1;
        if (num6 == num1 || num6 == num2 || num6 == num3 || num6 == num4 || num6 == num5)
        {
            num6 = rand.nextInt((6-1) + 1) + 1;
            System.out.println(num6);
        }
        else
        {
            System.out.println(num6);
        } 

    }

}
Halvor Holsten Strand
  • 19,829
  • 17
  • 83
  • 99
  • You could use a do-while loop instead of an if, to generate a new number until it matches your condition. However, you really need to look into using an array or set for this. – that other guy Sep 10 '14 at 01:10
  • Can't you use arrays or something? When you see too much repeated code, you probably can simplify your problem, and that's the case. – Hugo Sousa Sep 10 '14 at 01:10
  • 1
    Possible [duplicate](http://stackoverflow.com/q/8115722/230513). – trashgod Sep 10 '14 at 01:21

4 Answers4

2

You could put your numbers in a ArrayList. Then get a random number between 0-5 and print and then remove the number at that position from the ArrayList. That will leave only 5 numbers, get the next random number between 0-4, print and remove the number at that random index, so on and so forth till you only have one and just print that one.

Rob
  • 2,618
  • 2
  • 22
  • 29
1

There is a simpler way to approach this problem. Let's restate exactly what you want to do.

The Problem

You want to create n random numbers where for a given number k is not in the previous k-1 numbers

The Solution

Create a Set for holding random numbers, when you create a new one, add it to the set if and only if it is not already in the Set, otherwise generate a new random number and repeat.

Code Example

import java.lang.String;
import java.util.Random;
import java.util.Set;
import java.util.HashSet;

public class RandomNum {

    public static final void main(String[] args){
        final Random r = new Random();
        final Set<Integer> s = new HashSet<>();
        for(int i = 0; i < 6; i++){
                while(true) {
                int num = r.nextInt(6) + 1;
                if (s.contains(num) == false) {
                    s.add(num);
                    System.out.println(num);
                    break;
                }
            }
        }
    }
}

Output

/tmp$ javac RandomNum.java
/tmp$ java RandomNum
5
1
3 
2
4
6

Other Options

In principle you could use other data structures to hold the generated random numbers, such as a List. A Set has the nice advantage of being relatively quick to search in case you want to generate more than 6 values. You could get similar performance with an ArrayList. However the use of a Set has one more important advantage, it is reflects the semantics of what you intend (an unordered non-repeating set of values). You don't care about the ordering in the collection and you don't want duplicate values. A List is an ordered collection of values, a Set is an unordered set of values (meaning the elements can't be duplicated in the Set and they have no relation to each other with respect to the when they were added to theSet). When coding it is almost always a good idea to choose data structures that don't just work but reflect the semantics of what you mean.

isomarcte
  • 1,981
  • 12
  • 16
0

Small question: Why do you do rand.nextInt((6-1) + 1) instead of rand.nextInt(6)?

Now, about the task at hand: what I would is start from a list of possible numbers; choose randomly one of the existing indexes in the list (the result being the number at that index); remove the number from the list. When the list is empty, you can no longer take a number.

The nice thing about that solution is that you don't have to re-try several times generating a random number until you find one that hasn't already been used.

Exemple code :

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Main {

    public static void main(String[] args) {
        final List<Integer> integers = new ArrayList<>();
        final Random rand = new Random();

        // Fill list
        for (int i = 1; i <= 6; i++) {
            integers.add(i);
        }

        // Take numbers in random order
        while (!integers.isEmpty()) {
            final int index;
            if (integers.size() > 1) {
                index = rand.nextInt(integers.size() - 1);
            } else {
                // rand.nextInt(0) would throw an exception rather than return 0
                index = 0;
            }
            final int result = integers.remove(index);
            System.out.println("Result: " + result);
        }
    }
}
Cyäegha
  • 4,191
  • 2
  • 20
  • 36
0

You can do it as

public class NonRepreatedRandomNumbers {

    public static void main(String[] args) {
        ArrayList myList = new ArrayList();
        Random rd = new Random();
        int num;
        while(true){
            num = rd.nextInt(6) + 1;
            if(myList.size() ==  6)
                break;
            if(!myList.contains(num)){
                myList.add(num);
                System.out.println(myList);
            }else
                continue;
        }
    }
}
Hanzallah Afgan
  • 716
  • 6
  • 23