0
public class startOrder {


public static void main(String[] args) {
int[] start = new int[45];

for(int i=0;i<start.length;i++)
{
    start[i] = (int)(Math.random()*45);
}

for(int i=0;i<start.length;i++)
{
    System.out.println(start[i]);
}
}

}

I want to make a simple program that just prints all random numbers between 1-45. The problem though is there's duplicate numbers among them, how can I make it to print numbers between 1-45 without any duplication. It's a program that generates a random start number.

  • Just add every already generated number to an array / list and check with `contains()` if that number has already been generated once - then skip it. Note: This may result in BAD performance if you like to generate random numbers from 1 to 40, while having bounds from 1 to 41. – dognose Apr 06 '14 at 19:58

7 Answers7

4

Fill a list with the preferred numbers, then shuffle it, for example using the build in library function like below.

List<Integer> myNumbers = Arrays.asList(1,2,3,4, etc.);
Collections.shuffle(myNumbers);

Then simply iterate over the list.

Btw, this is called a random permutation.

wvdz
  • 16,251
  • 4
  • 53
  • 90
  • +1 This is the only answer that doesn't involve generating unnecessary random numbers for an unknown amount of time, losing efficiency. – leigero Apr 06 '14 at 20:17
0

You could fill a linked list with 1-45, and repeatedly remove one randomly selected index from the list.

earldouglas
  • 13,265
  • 5
  • 41
  • 50
0

Create an array or some collection, populate it with the numbers, then shuffle it using two randoms.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
0
  1. Create a boolean array bArray of size 45 with all places set to false (which means that number is not picked yet).
  2. Whenever you pick a number, let's say you picked 15, check if bArray[15] is false. If yes, pick that number and assign the bArray[15] to true (which means the number is now taken).

Repeat this till you have got all 45 numbers!

0

You can use a HashSet!

public static void main(String[] args)
{
    Set<Integer> start = new HashSet<Integer>();

    while (start.size() <= 45)
    {
        start.add((int) (Math.random() * 45)+1);
    }

    for (int i: start)
    {
        System.out.println(i);
    }
}

A Set does not allow duplicates, so when you reach a size of 45, you have all unique numbers between 1 and 45! Also keep in mind that your random code generated a number between 0-44, which I fixed.

Azar
  • 1,086
  • 12
  • 27
0

Just use Set as such

Set<Integer> start = new HashSet<Integer>();

Set's are designed to eliminate duplicate values

Maciej Cygan
  • 5,351
  • 5
  • 38
  • 72
0

You should look at the TreeSet and HashSet. These collections will not store any duplicates.

Read more about them here:

HashSet ---> http://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html

TreeSet ---> http://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html

Also, read in another Stackoverflow post about HashSet vs TreeSet, I highly recommend reading it. Read the top comment: Hashset vs Treeset

Community
  • 1
  • 1
UserFuser
  • 110
  • 12