# Random number generator between a certain range outputs the same number twice sometimes

I am using the following code to generate a random 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; } `

and am using it like this:

` int count = randInt(1, 4); System.out.println(count); `

But it intermittently displays the same number twice, and sometime even 3 times in a row. Is there any way to do a check if the last generated number is the new generated one and change it to next next random and so on?

thanks.

If you don't like the random number that was generated you can call you function again until a different number comes up:

`// set "last" and "count" somehow ...`

``` // keep generating random numbers until a new one comes up while ((count = randInt(1,4)) == last); last = count; ```

```// ... ```

Generally speaking, there is no reason for the same number not coming up twice. It's the same as rolling a dice.

And a heads up: don't try this with a range of 1 :-)

But it intermittently displays the same number twice, and sometime even 3 times in a row.

Sure. Your range is very narrow. If I am doing the math properly, there is a 25% chance (one chance in four) that you will have two of the same number in a row, and a ~6% chance (once chance in sixteen) that you will have three of the same number in a row. This is not significantly different than flipping a coin and having it come up "heads" twice or three times in succession.

Is there any way to do a check if the last generated number is the new generated one and change it to next next random and so on?

Hold onto the last value and check. Here is some rough-cut Java:

```class NonRepeatRandom { Random r=new Random(); int lastValue=-1;```

``` int randInt(int min, int max) { int result; do { result = rand.nextInt((max - min) + 1) + min; } while (result==lastValue); lastValue=result; ```

```return(result); } } ```

The problem is that you are instancing the Random class time and again, this kills the randomness as the seed of both instances can be virtually the same. Remember that nothing in a computer is random, the Random class depends on a seed derived from the actual system time that is mutated deterministically every time you request a new number.

Just hold on to an instance/class-level variable of the Random class and your problem will be solved.

By selecting a random number between 1 and 4 you always have a 25% chance of getting the individual number no matter what the last one was.

If you don't want to get random numbers you can do something like this

```function getRandomNumber() { do { \$n = rand(1,4); }while(in_array(\$n, array(2)); return \$n; } ```

To eliminate the slightest chance of this happening:

```Random random = ... int firstRandom = random.nextInt(20); int secondRandom = random.nextInt(20); while(firstRandom == secondRandom) secondRandom = random.nextInt(20); //Now, there is no chance they will be the same :- ) ```

Also keep in mind that the numbers may seem random to us, but they are not random at all. Have a look here: http://www.javamex.com/tutorials/random_numbers/java_util_random_algorithm.shtml#.VYlfsfntlHw

EDIT: If you want to be efficient and decrease the chances of the same number apearing twice, use an if statement instead:

```Random random = ... int firstRandom = random.nextInt(20); int secondRandom = random.nextInt(20); if(firstRandom == secondRandom) secondRandom = random.nextInt(20); //Although it may be again the same here, it is unlikely, especially when the limits are large like between 0 and 1000. ```

Category:java Views:1 Time:2019-01-09
Tags: java android