xxxxxxxxxx
// Smart Fast
static void random_array(int array[] , int N){
for (int i = 0 ; i < N ; ++i) {
int rand = (int) (Math.random() * N) ;
array[i] = rand ;
}
}
public static void main(String[] args){
int N = 10 ;
int[] value = new int[N] ;
random_array(value , N) ;
for(int j = 0 ; j < value.length ; ++j){
System.out.println(value[j]) ;
}
}
xxxxxxxxxx
import java.util.Random;
public class RandomStringFromArray
{
public static void main(String[] args)
{
String[] arr={"1", "2", "3", "4", "5"};
Random r=new Random();
int randomNumber=r.nextInt(arr.length);
System.out.println(arr[randomNumber]);
}
}
xxxxxxxxxx
import java.util.Random;
public class Example {
public static void main(String[] args) {
Random rd = new Random(); // creating Random object
int[] arr = new int[5];
for (int i = 0; i < arr.length; i++) {
arr[i] = rd.nextInt(); // storing random integers in an array
System.out.println(arr[i]); // printing each array element
}
}
}