1

How does one make it so a random number generator does not repeat numbers over? I tried this but it keeps giving me repeated numbers

public int random(int a, int b,int c,int d,int e,int f,int g,int h,int i,int j,int k,int l){
 Random generator = new Random();
 int choice = generator.nextInt(12) + 1;
 if((choice!=b)|(choice!=c)|(choice!=d)|(choice!=e)|(choice!=f)|(choice!=g)|(choice!=h)|(choice!=i)|(choice!=j)|(choice!=k)|(choice!=l)){
     a=choice;
 }
 else{
     random(a,b,c,d,e,f,g,h,i,j,k,l);


 }
 System.out.println(choice);
return a;
Joel Diaz
  • 79
  • 2
  • 7
  • See : http://stackoverflow.com/questions/4040001/creating-random-numbers-with-no-duplicates – zakinster Apr 26 '13 at 08:15
  • What do you exactly want to accomplish? Do you want a random number generator that is like "crossing out" the numbers already rolled for you, or do you think that the generator you habe is not random but repeating itself? Its not very clear – LionC Apr 26 '13 at 08:15

5 Answers5

2

It depends how many it should generate before repeating because if you completely disallowed all repeats you must eventually run out.

If you just need a few dozen, create an array of sequential numbers and shuffle them.

If you need a much longer sequence you will need to record them as they are generated and whenever you see a duplicate, request another one. A HashSet would do fine here.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0

You want to sample without replacement. Put all the a,b,...,l in a List<int> of size 12. Pick random elements from the list and remove them.

Nishanth
  • 6,932
  • 5
  • 26
  • 38
0

This is simple quick solution. Of course it may be better:

import java.util.*;

public class RandomNumberGenerator {

    private ArrayList<Integer> usedNumbers;
    private Random r;
    private int maxNumber;

    public RandomNumberGenerator(int maxNumber) {
        r = new Random();
        usedNumbers = new ArrayList<Integer>();
        this.maxNumber = maxNumber;
    }

    public int next() {
        Integer res = r.nextInt(maxNumber);
        if (usedNumbers.contains(res)) {
            next();
        }
        usedNumbers.add(res);
        if (usedNumbers.size()==maxNumber) {
            usedNumbers.clear();
        }
        return res;
    }

}
pepuch
  • 6,346
  • 7
  • 51
  • 84
  • don't you think you'll require to catch result of recursive next() call in 'res'. – VishalDevgire Apr 26 '13 at 08:32
  • I've tested that and it works. I agree that this code is not finally and it may contain some mistakes. As I wrote there may be a better solution as everywhere. – pepuch Apr 26 '13 at 08:34
0

The |, or, evaluation will check to see if any one of those != evaluations are true and then proceed to a=choice.

If you were to use the && evaluation instead, it would check to see if all of the the != evaluations are false, and if even one of them is true it skip down to your else.

So say you numbered your int a, int b.... as 1,2,...12- Since the only one you are not evaluating not equals for is a, it would only ever get to the a=choice when they both are 1. Which is pretty weird, to see if two ints are equal just to make them equal!

Others above have already given excellent choices on how to actually get a non repeating random number so I will refrain from repeating them.

ChrisWilson4
  • 195
  • 4
  • 23