Hey. I consistently generate 4 numbers randomly in the range from 1 to 8. But sometimes, of course, the generation is the same and some of the 4 numbers may be equal. How can you make sure that exactly all of the different ones are generated?
At first I wanted to use a while loop, but I don’t know how to make a condition for the simultaneous equality of 4 numbers. And besides, the program will be long running.

  • And to generate one, and then to get from it (say, adding 1) the remaining 3 is not good for some reasons? - avp
  • one
    they will not be completely "random". - KoVadim

4 answers 4

Put all generated numbers in the Set and look at its size. If after adding 4 numbers in Set 'e less than 4 elements, it means that some numbers coincide and you need to generate new ones. Here is an example of the code (the code demonstrates the idea, I don’t answer for working)

 Set<Integer> numbers = new HashSet<Integer>(); Random r = new Random(); while (numbers.size() < 4) { numbers.add(r.nextInt()); } 
  • eight
    I think that for 4 numbers it is more expensive than running through the array with a maximum of 3 elements each time. - avp

Make up in advance all possible variants of sequences of numbers (there are only 8 * 7 * 6 * 5 = 1680), generate a random number from 0 to 1679. The maximum productive version.

  • 2
    @Veikedo, precalc + O (1). Can you go faster? - Nofate
  • one
    > And how would you call a person who makes such objections to your decision "2 + 3 = 5"? I would call it "customer". This is quite a common situation when the development of software product requirements change, including after the previous requirements were met. In a good way, the program should be designed so that the programmer is as ready as possible for possible changes. On the contrary, a programmer who is rigidly tied up on certain conditions is hardly qualified. And all subsequent conversations regarding "but before it was different" - this is in favor of the poor - DreamChild
  • one
    +1 combinatorial perfect option. @DreamChild, in favor of the poor is ascii vs unicode, x86 vs x64, instead of thirteen cards in the suit of a million, not a six-sided cube and not two and not five, but a hundred hundred-sided ... overriding it. - Yura Ivanov
  • one
    @VadimTukaev, with KISS, you, too, probably know each other. - avp
  • one
    Then everything will depend on the number of required numbers. If there are 4 of them, and it is known that the number will not change (for example, these are seasons), then I like this option: int a, b, c, d; a = rand (); do {b = rand (); } while (b == a); do {c = rand (); } while (c == a || c == b); do {d = rand (); } while (d == a || d == b || d == c); One of the cases when the Chinese code is fully justified and even beautiful. - VadimTukaev

Something like this:

 int[] numbers = new int[4]; int number, j, i=0; Random r=new Random(); while(i < 4) { number=r.nextInt(); for(j=0; j < i; j++) { if(numbers[j]==number) break; } if(j == i) numbers[i++]=number; } 
  • 2
    If I understand correctly, then if(j == i) - Veikedo
  • Yes, that's right :) - changed - Barmaley

A little c # will not hurt anyone

 var rand = new Random(); var list = new List<int>(); while(list.Count < 4) { int r = rand.Next(); if(list.All(x => x != r)) { list.Add(r); } }