Randomly selecting an element from an array

0 votes

How do I randomly select a number from an array? Please guide as I am new to this.

Oct 11, 2018 in Java by v.liyyah
• 1,300 points
49 views

1 answer to this question.

0 votes

For randomly selecting an element, java.util.Random provides a random generator which you can use as below:

public static int getRandom(int[] array) {
    int rdm_num = new Random().nextInt(array.length);
    return array[rdm_num];
}
answered Oct 11, 2018 by anto.trigg4
• 3,440 points

Related Questions In Java

0 votes
2 answers

How can we remove an element from an array in Java?

You can use ArrayUtils class remove method which ...READ MORE

answered May 24, 2018 in Java by UshaK
472 views
0 votes
3 answers

How to rotate an array from a particular index?

Muchas gracias. ?Como puedo iniciar sesion? READ MORE

answered May 2 in Java by oisoucknfn
157 views
0 votes
1 answer

How to remove object from an Array?

Here is a code I came up ...READ MORE

answered Mar 11, 2019 in Java by Esha Gupta
75 views
0 votes
2 answers

How to convert an array to arraylist?

In Java 9 you can use: List<String> list= List.of("Hello", "World", ...READ MORE

answered Aug 1, 2018 in Java by samarth295
• 2,190 points
107 views
0 votes
2 answers

How to test that an array contains a certain value?

public static final String[] VALUES = new ...READ MORE

answered Jul 17, 2018 in Java by Sushmita
• 6,890 points
107 views
0 votes
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,580 points
156 views
0 votes
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,280 points
159 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
1,694 views
0 votes
2 answers

What is the syntax to initialize an array?

Rather than learning un-Official websites learn from ...READ MORE

answered Aug 2, 2018 in Java by samarth295
• 2,190 points
119 views
+1 vote
2 answers

Retrieve all the implementations of an interface in Java

Take a look to this example: https://github.com/burningwave/co ...READ MORE

answered Dec 20, 2019 in Java by Roberto
• 370 points

edited Jul 6 by Roberto 7,813 views