What is the best way to get four unique random numbers from 0 to 9?

I want to generate four random numbers ranging from 0 to 9. It is easy to generate four random numbers with the Java Random class.

Random random = new Random(); int numbers[] = new int[4]; for(int i=0;i<4;i++){ numbers[i] = random.nextInt(10); } 

With this, I can easily get an array of four numbers, 9369 , 4702 , etc. In this case, it may be possible to repeat a number in four numbers, and I do not want this to be repeated in numbers.

Here I want all four digits in the above array to be unique so that I can get output, like 9543 , 1234 , etc.

For this, I thought about the following.

  • Create a random number and assign it as the first number.
  • Create a random number and check with the first number, if another is assigned the second number, generate a random number again and repeat, etc.

Is there a better way than the method above so that I can quickly and quickly get four unique random numbers?

Any suggestion is welcome.

+8
java random numbers
source share
5 answers

You can use Collections.shuffle :

 // generate a List that contains the numbers 0 to 9 List<Integer> digits = IntStream.range(0,10).boxed().collect(Collectors.toList()); // shuffle the List Collections.shuffle (digits); // take the first 4 elements of the List int numbers[] = new int[4]; for(int i=0;i<4;i++){ numbers[i] = digits.get(i); } 
+31
source share

You can use Set for this, the idea is to generate your random number, then put it in the set and continue to do this until you have 4 elements in your set, when you have 4 unique random numbers stored in your set

 Set<Integer> randomSet = new HashSet<>(); while(randomSet.size() <4) randomSet.add //add your generated random number 
+9
source share

If you can create a fast function f that maps natural numbers to a set of numbers that meet your requirements, you can only create one random number. Your lead time is limited f. If you can create f fast enough, this is the most efficient way to do this.

The simplest solution would be to put all the numbers that meet your criteria in the array and create a random number as an index into that array. → O (1)

+7
source share

As you can see, there are many ways to achieve your goal. Here is my suggestion

 Random random = new Random(); // prepare all valid digits List<Integer> from = new ArrayList<Integer>(Arrays.asList(0,1,2,3,4,5,6,7,8,9)); // take set in an random order int numbers[] = new int[4]; for(int i = 0; i < numbers.length; i++){ numbers[i] = from.remove (random.nextInt (from.size())); } for (int num : numbers) { System.out.println(num); // when you prefer this } 
+7
source share

EDIT

Since Collections.shuffle also uses fish-yates algorithm. But this option selects the starting point of the radomly sequence. This is like shuffling a deck of cards and picking 4 cards from the middle versus shuffling a deck of cards and picking 4 on top.

Here's a use case for the Fisher-Yeats shuffling algorithm here https://softwareengineering.stackexchange.com/questions/199644/efficient-way-to-shuffle-objects

  public int[] shuffle() { int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; Random r = new Random(); for (int i = a.length; i > 1; i--) { swap(a, i - 1, r.nextInt(i)); } int[] result = new int[4]; // Variant :: Randomly choosing the starting point of the // sequence, since we need only four number. System.arraycopy(a, r.nextInt(a.length - 4), result, 0, 4); return result; } private void swap(int[] a, int i, int i1) { int temp = a[i]; a[i] = a[i1]; a[i1] = temp; } 

Link: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

0
source share

All Articles