1

I found answers on how to generate random numbers but nowhere how to generate all the numbers in the range without duplication in Java. Please share if you have a solution. Below is what I did but it simply generates randomly the numbers. I need to print out all numbers in the range without duplication!

package com.company;

import java.util.*;


public class RandomizeNumbers {

    public static void main(String[] args) {

        //Create Scanner
        Scanner userInput = new Scanner(System.in);

        //Ask for numbers N and M
        System.out.println("Please enter two numbers and the program will randomize the numbers between them. " +
                "The first number N must be bigger or equal to the second number M");
        System.out.println("Please enter the first number N");
        int n = userInput.nextInt();
        System.out.println("Please enter the second number M");
        int m = userInput.nextInt();
        Random randomGenerator = new Random();
        int difference = n - m;
        //Randomize the numbers
        if (m<=n){
            for(int i = 0; i<= difference; i++ ) {
                int randomInt = randomGenerator.nextInt(n - m + 1) + m;
                System.out.println(randomInt);
            }
        }
            else{
            System.out.println("Please enter M less or equal to N");
        }

    }
}
Arc676
  • 4,445
  • 3
  • 28
  • 44
StMit
  • 13
  • 3
  • 4
    Create a List for the range `m-n`, shuffle the list and loop through the list – SomeJavaGuy Oct 26 '15 at 08:51
  • 2
    1) Add all the numbers to an array. 2) [Shuffle](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) the array. – Phylogenesis Oct 26 '15 at 08:52
  • All the integer random numbers between two integers are all the integer number between two integer, yo do not need to generate them, you could randomly permutate them. – il_raffa Oct 26 '15 at 09:00
  • Is it possible this to be done without any collections - no Lists or Arrays? – StMit Oct 26 '15 at 09:27
  • I did it with the Array. Thank you. However, I still think there must be a way to avoid the Collections – StMit Oct 26 '15 at 09:35

5 Answers5

2

What you need maybe generating a random permutation, pls see this link How to generate a random permutation in Java?

Community
  • 1
  • 1
throwit
  • 714
  • 3
  • 13
2

You can store generated number in a array.then after generate the next number check is there this number in array or no.

Ahmad Azarnia
  • 129
  • 1
  • 1
  • 11
1

There are many ways to achieve this, lets suppose you want 50 numbers between A and B, then use a java.util.Set, since this collection does "ignore" duplicated values: following snippet describe it better:

Set<Integer> setA = new HashSet<Integer>();
    Random r = new Random(System.currentTimeMillis());
    int low = 10;
    int high = 100;
    int rnd = r.nextInt(high - low + 1) + low;
    int maxCount = 50;

    while (setA.size() < maxCount ) {  //<--how many random numbers do you need?
        rnd = r.nextInt(high - low + 1) + low;
        setA.add(rnd);
    }
   

and be careful, not to get in an infinite loop. (there are only "B-A" possible integer options between A and B, so MaxCount<= B-A)

Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

What I suggest you to do is to create a List and then shuffle it.

ArrayList<Integer> list = new ArrayList();

int high = 20;
int low = 10;

for(int i = low; i <= high; ++i)
 list.add(i);

Collections.shuffle(list);

And then create a function to get a random Unique number each time.

static int index = 0;

public int roll(ArrayList<Integer> list)
{
  return  list.get(index ++);
}
Uma Kanth
  • 5,659
  • 2
  • 20
  • 41
0

You can put all the numbers between n & m into a list and then use Collections.shuffle(list) to make the numbers ordered randomly in the list.

if (difference > 0) {
        List<Integer> integers = new ArrayList<>();
        for (int i = 0; i <= difference; ++i) {
            integers.add(m + i);
        }

        Collections.shuffle(integers);

        for (Integer randNum : integers) {
            System.out.print(randNum + "\t");
        }
        System.out.println();
    } else {
        System.out.println("Please enter M less or equal to N");
    }
Bon
  • 3,073
  • 5
  • 21
  • 40