how to pick up value from array in java

693 views Asked by At

i have an array it fill with some values(int).i want to pick one of this elements up randomly and use it value as a index of another array.what should i do?

int value[] = {1,2,3,8,9,40,0,5,...};
int value-2[] = {0,1,2};
Random rand = new Random();
System.out.print(value-2[rand.nextint(value.lenght)]);
1

There are 1 answers

0
ludacris2k4 On

Create arrays:

int[] array = {1, 2, 3, 4};
int[] array2 = {1, 2, 3, 4};

Generate a random index between 0 and array.length:

int idx = new Random().nextInt(array.length-1);

Get value from first array:

String random = array[idx];

Get value from second array:

String value = array2[random];