How to randomly select an element from an array

I am looking for a solution to randomly select a number from an integer array.

For example, I have an array new int[]{1,2,3} , how can I choose a number randomly?

+45
java
Nov 09 '11 at 13:13
source share
11 answers
 public static int getRandom(int[] array) { int rnd = new Random().nextInt(array.length); return array[rnd]; } 
+93
Nov 09 '11 at 13:15
source share

The random number generator can be used to generate a random index and return an element to this index:

 //initialization Random generator = new Random(); int randomIndex = generator.nextInt(myArray.length); return myArray[randomIndex]; 
+8
Nov 09
source share

If you are going to receive a random element several times, you want your random number generator to be initialized only once.

 import java.util.Random; public class RandArray { private int[] items = new int[]{1,2,3}; private Random rand = new Random(); public int getRandArrayElement(){ return items[rand.nextInt(items.length)]; } } 

If you select random array elements that should be unpredictable, you should use java.security.SecureRandom , not Random. This ensures that if someone knows the last few choices, they will not have the advantage of guessing the next one.

If you want to select a random number from an Object array using generics, you can define a way for this (Source Avinash R in Random element from an array of strings ):

 import java.util.Random; public class RandArray { private static Random rand = new Random(); private static <T> T randomFrom(T... items) { return items[rand.nextInt(items.length)]; } } 
+6
Apr 02 '16 at 10:33
source share

use java.util.Random to generate a random number between 0 and the length of the array: random_number , and then use a random number to get an integer: array[random_number]

+3
Nov 09
source share

Use the Random class:

 int getRandomNumber(int[] arr) { return arr[(new Random()).nextInt(arr.length)]; } 
+2
Nov 09
source share

you can also use

 public static int getRandom(int[] array) { int rnd = (int)(Math.random()*array.length); return array[rnd]; } 

Math.random () returns an int between 0.0 (inclusive) to 1.0 (exclusive)

multiplying this by array.length, you get a number between 0.0 (inclusive) and array.length (exclusive)

casting to int will be rounded, giving you both an integer from 0 (inclusive) and array.length-1 (inclusive)

+1
Nov 09 '11 at 13:23
source share

Java has a Random class in the java.util package. Using it, you can do the following:

 Random rnd = new Random(); int randomNumberFromArray = array[rnd.nextInt(3)]; 

Hope this helps!

0
Nov 09 '11 at 13:18
source share

Take a look at this question:

How to generate random integers in a specific range in Java?

You want to create a random number from 0 to integers - 1. Then just get your int from your array:

 myArray[myRandomNumber]; 
0
Nov 09 '11 at 13:18
source share
 Collections.shuffle(Arrays.asList(araay_of_int)); 

The above is the best way to accomplish your task.

0
Jan 28 '13 at 7:55
source share

Since you have java 8, another solution is to use the Stream API.

 new Random().ints(1, 500).limit(500).forEach(p -> System.out.println(list[p])); 

Where 1 is the lowest int generated (inclusive), and 500 is the highest (exceptional). limit means your thread will have a length of 500.

  int[] list = new int[] {1,2,3,4,5,6}; new Random().ints(0, list.length).limit(10).forEach(p -> System.out.println(list[p])); 

Random from java.util package.

0
Sep 21 '15 at 11:24
source share
 package workouts; import java.util.Random; /** * * @author Muthu */ public class RandomGenerator { public static void main(String[] args) { for(int i=0;i<5;i++){ rndFunc(); } } public static void rndFunc(){ int[]a= new int[]{1,2,3}; Random rnd= new Random(); System.out.println(a[rnd.nextInt(a.length)]); } } 
0
Mar 06 '16 at 13:56 on
source share



All Articles