1

i do like it :

    int randOne = 3;
    int randTwo = 4;
    int oneNubmer;
    int twoNumber;

    do {
         oneNubmer = 1 + (int) (Math.random() * randOne);
         twoNumber = 1 + (int) (Math.random() * randTwo);
    } while (oneNubmer == twoNumber);

It works, but increases operation time greatly, because this function is called 1000+ times.

How to do it better?

4
  • 2
    Note: Random numbers without duplicates aren't truly random, in all cases, because you may have to discard one or more of the draws. Commented May 3, 2019 at 11:07
  • @TimBiegeleisen Biegeleisen What do you have in mind? Commented May 3, 2019 at 11:08
  • Maybe you want "unique" numbers instead of "random" numbers. Check java.util.UUID and maybe stackoverflow.com/questions/15184820/… Commented May 3, 2019 at 11:15
  • @ Marteng yes, but i need unique numbers from 1 to 4 and from 1 to 5. Commented May 3, 2019 at 11:16

1 Answer 1

2

As soon as you impose certain rules to the numbers being generated (eg they cannot be duplicates), you can't really consider them random anymore.

You could consider adding oneNumber to twoNumber (or any other operation where oneNumber is used in the calculation of twoNumber), that way they'll never be equal. Although this again imposes the fact that twoNumber will always be higher then oneNumber, so could again be seen as a breach in the 'Random' concept.

oneNumber = 1 + (int) (Math.random() * randOne);
twoNumber = 1 + oneNumber + (int) (Math.random() * randTwo);

For your requirement where you want to generate the first number between 1-4 and second number between 1-5 without duplicates you could use following approach:

// Create a list containing numbers 1 till 5
List<Integer> numbers = new ArrayList<Integer>();
for(int i = 1; i < 6; i++) {
  numbers.add(i);
}

Random random = new Random();

// Randomly get the index of your first number, this will be a number 
// between 1 and 4
int firstIndex = random.nextInt(4);
int number1 = numbers.get(firstIndex);

// Remove that number from the list, your list now becomes size 4, and no 
// longer contains the first number you picked.
numbers.remove(firstIndex);

// Randomly get the index of your second number, this will be a number 
// between 1 and 5 without the number picked earlier.
int secondIndex = random.nextInt(4);
int number2 = numbers.get(secondIndex);

System.out.println(number1);
System.out.println(number2);

Or perhaps cleaner and faster:

// Create a list containing numbers 1 till 5 -> might want to extract this
// from the method so you don't have to rebuild the array over and over 
// again each call...
List<Integer> numbers = new ArrayList<Integer>();
for(int i = 1; i < 6; i++) {
  numbers.add(i);
}

// Shuffle the array randomly
Collections.shuffle(numbers);

// Get the first 2 numbers from the array
int number1 = numbers.get(0);
int number2 = numbers.get(1);

// If number1 equals 5, swap number1 and number2 as you want number1 to be 
// 1-4 and number2 to be 1-5
if(number1 == 5) {
  number1 = number2;
  number2 = 5;
}

System.out.println(number1);
System.out.println(number2);
Sign up to request clarification or add additional context in comments.

8 Comments

thanks, but i , but i need unique from 1 to 4 for first int and from 1 to 5 for second int. No more no less
Edited my response with a suggestion for your specific situation.
The problem is that 5 will be more often than others
True, but that's an implicit consequence of your specific requirement. Since 5 can only occur as second number, and it can never be duplicate to the first, it will occur slightly more often.
Of course you'll have to rewrite that part a bit too match your new requirements... :-) The basic idea is that with the above approach you can have x unique numbers in a specific range. How you want to deal with them, order them or process them further is fully up to you.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.