# Generating UNIQUE Random Numbers within a range

I need to generate random UNIQUE numbers within a range, how can I do that? I can generate random number by

``````generator:
\$arr = [];
\$x = rand(\$min, \$max);
\$len = count(\$arr);
\$flag = 0;
for(\$i = 0; \$i < \$len; \$i++)
{
if (\$flag === 1)
goto generator;
if (\$x === \$arr[\$i])
\$flag = 1;
}
\$arr[\$index] = \$x;
\$index++;
goto generator;
``````

I know this code is bad, so I need a better optimized code of my version ! help !

example: if i need to generate 3 numbers within 1 to 15 they should be like 5, 9, 1 but not 3,1,2 [with in 1 - 3 (numbers i want to generate) ]

12

Array with range of numbers at random order:

``````\$numbers = range(1, 20);
shuffle(\$numbers);
``````

Wrapped function:

``````function UniqueRandomNumbersWithinRange(\$min, \$max, \$quantity) {
\$numbers = range(\$min, \$max);
shuffle(\$numbers);
return array_slice(\$numbers, 0, \$quantity);
}
``````

Example:

``````<?php
print_r( UniqueRandomNumbersWithinRange(0,25,5) );
?>
``````

Result:

`````` Array
(
 => 14
 => 16
 => 17
 => 20
 => 1
)
``````
Wednesday, March 31, 2021

86

You should point to your `vendor/autoload.php` at `Settings | PHP | PHPUnit` when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario: http://confluence.jetbrains.com/display/PhpStorm/PHPUnit+Installation+via+Composer+in+PhpStorm

Related usability ticket: http://youtrack.jetbrains.com/issue/WI-18388

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021

79

On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.

Note that this solution will not work on Mountain Lion (10.8).

Saturday, May 29, 2021

45
• Add each number in the range sequentially in a list structure.
• Shuffle it.
• Take the first 'n'.

Here is a simple implementation. This will print 3 unique random numbers from the range 1-10.

``````import java.util.ArrayList;
import java.util.Collections;

public class UniqueRandomNumbers {

public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i=1; i<11; i++) {
}
Collections.shuffle(list);
for (int i=0; i<3; i++) {
System.out.println(list.get(i));
}
}
}
``````

The first part of the fix with the original approach, as Mark Byers pointed out in an answer now deleted, is to use only a single `Random` instance.

That is what is causing the numbers to be identical. A `Random` instance is seeded by the current time in milliseconds. For a particular seed value, the 'random' instance will return the exact same sequence of pseudo random numbers.

NOTE that the `public Integer?(int value)` constructor is `deprecated` since Java 9.

The first for loop can simply be changed to:

``````for (int i = 1; i < 11; i++) {
}
``````
Tuesday, June 1, 2021

71

If you just need sampling without replacement:

``````>>> import random
>>> random.sample(range(1, 100), 3)
[77, 52, 45]
``````

random.sample takes a population and a sample size `k` and returns `k` random members of the population.

If you have to control for the case where `k` is larger than `len(population)`, you need to be prepared to catch a `ValueError`:

``````>>> try:
...   random.sample(range(1, 2), 3)
... except ValueError:
...   print('Sample size exceeded population size.')
...
Sample size exceeded population size
``````
Tuesday, June 1, 2021