Java - how to create a random number from an array of values

in the first iteration - I want to create a random number from 0..10

This is how I implement it -

int generator= (int)(Math.random() * (10)); 

In the first iteration, I want to create another random number, but without one of the values. For example, I would like a random number without number 4. only 1,2,3,5,6,7,8,9,0

How to remove one of the values ​​from the numbers 0..10 when generating a random number?

+4
source share
7 answers

The first way: -

You can also save a List all numbers. Then use Collections.shuffle to shuffle the list and get the first item. And delete it.

  List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(4); list.add(2); list.add(3); System.out.println("Original List: " + list); Collections.shuffle(list); System.out.println("Shuffled List: " + list); int number1 = list.remove(0); // Remove the first value System.out.println("Number removed: " + number1); Collections.shuffle(list); System.out.println("Shuffled List: " + list); number1 = list.remove(0); System.out.println("Number removed: " + number1); 

OUTPUT : -

 Original List: [1, 4, 2, 3] Shuffled List: [4, 3, 1, 2] Number removed: 4 Shuffled List: [1, 3, 2] Number removed: 1 

The second way: -

It is best to create a random number from 0 in the size list and get the value that index and delete it.

 int size = list.size(); Random random = new Random(); for (int i = 0; i < size; i++) { int index = random.nextInt(list.size()); int number1 = list.remove(index); System.out.println(number1); } 

NOTE: - If you want to exclude a specific set of numbers from the generated ones, then you can have these numbers in the list. Then remove all items from this list from the list of numbers . And then use any of the above methods.

+5
source

Random.nextInt(10)==number create again

 public int generateExcept(int number) { Random random = new Random(); int generated = 0; while ((generated = random.nextInt(11)) == number) { } return generated; } 

As indicated by OP

In the first iteration, I want to create another random number, but without one of the values

If I understand correctly, this is a one-time operation that is not repeated.

No regeneration

 public static int generateExcept(int number, int max) { Random random = new Random(); int generated = 0; if ((generated = random.nextInt(max - 1)) == number) { generated++; } return generated; } 

Random is always generated max-1 , because we want to increase by one

+1
source
 int generator= (int)(Math.random() * (10)); if (generator >= 4) generator++; 

This will include the number 10 as a possible result, if you do not want this, multiply by 9 instead of 10.

+1
source

You can save the values ​​in a list and create a random index that you need to take from this list.

 private Random random = new Random(); private ArrayList<Integer> values = new ArrayList<Integer>(); public static int nextInt() { if (values.size() > 0) { return values.remove(random.nextInt(values.size())); } else { throw new RuntimeException("no values possible"); } } 
+1
source

There are two main effective approaches:

1. Create an array of valid values ​​(from 0 to 10, with the exception of 4), and use a randomizer to select an index in this array. The following is an example:

 Random random = new Random(); int[] unwanted = {4}; int[] values = new int[10-unwanted.length]; for(int i=0; i<10-unwanted.length; i++){ for(int curr : unwanted){ if(i == curr) continue; } values[i] = i; } int result = values[random.nextInt(10-unwanted.length)]; 

2 - Approach the approach of Simon Andre .

+1
source
 int excluded[] = { 1,4,3}; Random r = new Random(); int generator; do { generator= r.nextInt(10); } while (! Arrays.asList(excluded).contains(generator) ) 
0
source

What you could do (maybe there is a cleaner way to handle this) is to check the 2nd generated number to see if it matches the first, and if so, just try running a random number. generator again. A simple if ... else loop should take care of this.

0
source

Source: https://habr.com/ru/post/1443066/


All Articles