-1

Here is the deal: I have a java code that generates a random number between two numbers and it works just fine. But i'm trying to achieve something. I want it in such a way that after the first number has been generated, when it recalls it, it doesn't generate that same number. For instance, if number 4 is generated, i want it now to be included among the possible generated numbers for the second time. To make things a little bit clear, here's my code.

      int Maximum=10;
      int Minimum=1;
      int r;
      r = (int)(Math.random() * Maximum) + Minimum;

is there anything i can do to make the code above not to generate a particular number between 1 and 10? Thanks.

Afam
  • 29
  • 1
  • 8
  • Have a look at the following link: http://stackoverflow.com/questions/14935997/generate-random-numbers-except-certain-values – NcDreamy Jun 07 '14 at 13:54
  • hold random number in a temporary variable and use an if check. if the next random number is same as the previous one, skip it. – Andrew Lobley Jun 07 '14 at 13:55
  • 5
    It looks like you're not generating random numbers, but drawing elements from a set without replacement. – Henk Langeveld Jun 07 '14 at 13:55
  • LinkedHashSet would guarantee, that you have no duplicates in your generated sequence, and keep the original order of generation. – mavarazy Jun 07 '14 at 14:03
  • This question doesn't make sense. If you're putting restrictions on a PRNG, it's not exactly going to be random anymore, is it? In fact the outcome is going to be incrementally more predictable. – Mikkel Løkke Jun 07 '14 at 14:14
  • @MikkelLøkke it's one of the most common questions about random numbers on this site - confusion between generating random numbers and randomly drawing elements from a range without replacement... – Boris the Spider Jun 07 '14 at 14:15

4 Answers4

2

You could fill a collection and simply removed a randomly selected number:

List<Integer> nums = new ArrayList<>();
for (int i = /* start */; i < /* end */; i++) {
    nums.add(i);
}

//Elsewhere
Random rand = new Random();
int get = nums.remove(rand.nextInt(/* end */);
/* end */--;

This may be a bit slower initially for large amounts of numbers but the end result is a lower time-complexity on your checks for new numbers (whereas a while loop could in theory be O(∞n) if I'm not mistaken)

Alternatively, use Collections#shuffle

Rogue
  • 11,105
  • 5
  • 45
  • 71
2

This doesn't seem like random number generation, it seems like randomly shuffling a Collection.

In Java 8:

public static void main(String[] args) {
    final int min = 0;
    final int max = 10;
    final List<Integer> nums = IntStream.rangeClosed(min, max).
            boxed().
            collect(toList());
    Collections.shuffle(nums);
    nums.forEach(System.out::println);
}

Output:

3
10
5
0
8
7
9
2
1
4
6

Each number [0, 10] appears only once in a random order.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
0

if you just want to exclude a single number replace this:

r = (int)(Math.random() * Maximum) + Minimum;

with this:

while( ( r = (int)(Math.random() * Maximum) + Minimum) == excludedNumber );
  • Pls, does excludedNumber need to be initialized? – Afam Jun 07 '14 at 14:14
  • This answer would not work unless you were excluding one (and *only* one) number. That, and it can have an infinite time complexity. – Rogue Jun 07 '14 at 16:12
-1
    ArrayList<Integer> intList = new ArrayList<Integer>();

    int Maximum=10;
    int Minimum=1;
    int r;
    int[] intArray = new int[6];


    for(int i=0; i<intArray.length; i++){

        r = (int)( Minimum+ (Math.random()* (Maximum-Minimum+1)) );

        if(!intList.contains(r)){
            intList.add(r);
            intArray[i]=r;
            System.out.println(r);
        }
        else{
            System.out.println(r+ "is a duplicate generated earlier, not putting in array again");
            i--;
        }

    }
Andrew Lobley
  • 342
  • 4
  • 12
  • `List` has `O(n)` contains. Also this is very inefficient way of achieving a very simple goal. In theory it might need to generate _infinity_ numbers to get one that hasn't been seen before. In practice, as we get towards the end of the range search time will increase. – Boris the Spider Jun 07 '14 at 14:09