3

I know for generating random number there is random function in java

for example 

       int randomNumber = ( int )( Math.random() * 9999 );

which will generate randomNumber from [0,9999] but it only returns one number by given range.

But I just want to know if there is any built in function or you can say native function which will generate more than one random numbers and it should also not match with each other

suppose from above example if i want to generate 4 number

it will return like 1,10,50,5544

Here you can see that there is a four random number and it is not matching with each other .

Procrastinator
  • 2,526
  • 30
  • 27
  • 36
Siddhpura Amit
  • 14,534
  • 16
  • 91
  • 150
  • 1
    See [this answer](http://stackoverflow.com/questions/9423523/generate-unique-random-numbers-in-java?rq=1) for a possible solution – Michael Lang Jul 11 '13 at 06:06
  • 1
    please take care of your tags, this here is a basic language question, completely unrelated to swing, java-me, java-ee or android (which I removed) – kleopatra Jul 11 '13 at 06:12
  • As I get Java .. they try to provide only basic functions in the core of the language. Getting N unique results out of the Core Random function can be done in 2-3 rows of simple code .. so no point to implement this as a separate function. – d.raev Jul 11 '13 at 06:15

6 Answers6

4

try something like this

Create an ArrayList and add your random number and while adding check if ArrayList already contains the number or not.

Eg:

ArrayList<Integer> numbers = new ArrayList<Integer>();
while (numbers.size()<=YOUR_MAX_SIZE)
{
    int randomInteger = ( int )( Math.random() * 9999 );
    if (!numbers.contains(randomInteger)) {
    {
       numbers.add(randomInteger);
    }
}
Michael Shrestha
  • 2,547
  • 19
  • 30
4

Another technique (besides the one shown by @MichaelShrestha) is to put the entire range of numbers in a collection, then shuffle it and take the numbers in the shuffled order.

It has the advantage that whereas the other method might spin though many (many) numbers to find a non-duplicate random value, this will only have to be run once. OTOH, for small collections of numbers, @Michael's technique might be faster.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
3

Try this, as proposed here:

int min = 1;
int max = 10000;

Random r = new Random();
int i1 = r.nextInt(max - min + 1) + min;

EDIT:

With no duplicates:

Random rng = new Random(); // Ideally just create one instance globally
int min = 1;
int max = 10000;
Set<Integer> generated = new LinkedHashSet<Integer>();
while (generated.size() < numbersNeeded)
{
    Integer next = r.nextInt(max - min + 1) + min;
    generated.add(next);
}

LINK

Community
  • 1
  • 1
g00dy
  • 6,752
  • 2
  • 30
  • 43
0

There is not built in api. but you can use something like this

public static int[] getRandoms(int amount, int range){
    int[] result = new int[amount];
    ArrayList tempList = new ArrayList();
    for(int i = 0; i< range; i++)
       tempList.add(i);
    Collections.shuffle(tempList);
    for(int i = 0; i< amount; i++)
       result[i] = (int)tempList.get(i);
    return result;
}

Here amount is the number of randome numbers you want and the range is the maximum range of random number

stinepike
  • 54,068
  • 14
  • 92
  • 112
0
    ArrayList<Integer> rnumbers = new ArrayList<Integer>(); 
    for (int i = 0; i < 4; i++) {
        int randomNumber = (int) (Math.random() * 9999);
        while (rnumbers.contains(randomNumber)) {
            randomNumber = (int) (Math.random() * 9999);
        }
        rnumbers.add(randomNumber);
    }
dijkstra
  • 1,068
  • 2
  • 16
  • 39
0

As HashSet contains unique object... You can have the unique random numbers.

Set uniqueRandomNumber = new HashSet();
 int i = 5 //number Of Random Number Required;
 for(int = 0; i<5; 1++){
    int randomNumber = ( int )( Math.random() * 9999 );
    uniqueRandomNumber.add(randomNumber);
 }
Vish
  • 832
  • 7
  • 21