Random(direct) access implies the ability to access any entry in a array in constant time (independent of its position in the array and of array's size). And that is big advantage. It is typically contrasted to sequential access.
What you propose is the best solution there is - choose a random index and then use the element at this index. If your question is how to get a random integer, use the built-in function rand() .
Use the numpy. random. choice() function to generate the random choices and samples from a NumPy multidimensional array. Using this function we can get single or multiple random numbers from the n-dimensional array with or without replacement.
public static int getRandom(int[] array) {
int rnd = new Random().nextInt(array.length);
return array[rnd];
}
You can use the Random generator to generate a random index and return the element at that index:
//initialization
Random generator = new Random();
int randomIndex = generator.nextInt(myArray.length);
return myArray[randomIndex];
If you are going to be getting a random element multiple times, you want to make sure your random number generator is 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 are picking random array elements that need to be unpredictable, you should use java.security.SecureRandom rather than Random. That ensures that if somebody knows the last few picks, they won't have an advantage in guessing the next one.
If you are looking to pick a random number from an Object array using generics, you could define a method for doing so (Source Avinash R in Random element from string array):
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)];
}
}
With Java 7, one can use ThreadLocalRandom
.
A random number generator isolated to the current thread. Like the global Random generator used by the Math class, a ThreadLocalRandom is initialized with an internally generated seed that may not otherwise be modified. When applicable, use of ThreadLocalRandom rather than shared Random objects in concurrent programs will typically encounter much less overhead and contention. Use of ThreadLocalRandom is particularly appropriate when multiple tasks (for example, each a ForkJoinTask) use random numbers in parallel in thread pools.
public static int getRandomElement(int[] arr){
return arr[ThreadLocalRandom.current().nextInt(arr.length)];
}
//Example Usage:
int[] nums = {1, 2, 3, 4};
int randNum = getRandomElement(nums);
System.out.println(randNum);
A generic version can also be written, but it will not work for primitive arrays.
public static <T> T getRandomElement(T[] arr){
return arr[ThreadLocalRandom.current().nextInt(arr.length)];
}
//Example Usage:
String[] strs = {"aa", "bb", "cc"};
String randStr = getRandomElement(strs);
System.out.println(randStr);
Use the Random class:
int getRandomNumber(int[] arr)
{
return arr[(new Random()).nextInt(arr.length)];
}
use java.util.Random
to generate a random number between 0 and array length: random_number
, and then use the random number to get the integer: array[random_number]
You can also use
public static int getRandom(int[] array) {
int rnd = (int)(Math.random()*array.length);
return array[rnd];
}
Math.random()
returns an double
between 0.0
(inclusive) to 1.0
(exclusive)
Multiplying this with array.length
gives you a double
between 0.0
(inclusive) and array.length
(exclusive)
Casting to int
will round down giving you and integer between 0
(inclusive) and array.length-1
(inclusive)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With