+ 5
How can I use this random number to select am index from an array?
I've been rolling this one around a little bit and I just can't quite make the connection. If I make an array of say 1000 indexes, and reduce the range of the rng to match that, how can I use the resulting number to call and display the selected index? https://code.sololearn.com/c6eVmt0l4qMr/?ref=app
3 Answers
+ 18
suppose u have an array of 10 elements , then u can have possible index values from 0 to 9 ...
//U want to print 5 randomly selected numbers from this array arr
Random rn=new Random();
for (int a=1;a <=5;a++){
int r=rn.nextInt(10)
System.out.println (arr []);
}
+ 1
Ok cool, thank you! Now what if I want to select only one element of that array at random? Change the 5 to 1? Ultimately I'm seeking to use the number from the rng to be what decides which index from a particular array is used