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?

Stephan Hogenboom
  • 1,543
  • 2
  • 17
  • 29
heavywar
  • 149
  • 10
  • 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. – Tim Biegeleisen May 03 '19 at 11:07
  • @TimBiegeleisen Biegeleisen What do you have in mind? – heavywar May 03 '19 at 11:08
  • Maybe you want "unique" numbers instead of "random" numbers. Check `java.util.UUID` and maybe https://stackoverflow.com/questions/15184820/how-to-generate-unique-positive-long-using-uuid – Marteng May 03 '19 at 11:15
  • @ Marteng yes, but i need unique numbers from 1 to 4 and from 1 to 5. – heavywar May 03 '19 at 11:16

1 Answers1

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);
TheWhiteRabbit
  • 1,253
  • 1
  • 5
  • 18
  • 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 – heavywar May 03 '19 at 11:20
  • 1
    Edited my response with a suggestion for your specific situation. – TheWhiteRabbit May 03 '19 at 11:30
  • The problem is that 5 will be more often than others – heavywar May 03 '19 at 12:01
  • 1
    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. – TheWhiteRabbit May 03 '19 at 12:10
  • And what if I want to add not two but three random numbers – heavywar May 03 '19 at 12:11
  • All depends on the specific requirements you have for that 3rd number then, but basically you just fetch the first 3 elements from the array. – TheWhiteRabbit May 03 '19 at 12:13
  • just difficult do (number1 == 5) { number1 = number2; number2 = 5; } with 3 number – heavywar May 03 '19 at 12:14
  • 1
    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. – TheWhiteRabbit May 03 '19 at 12:18