Random element from string array [closed] Random element from string array [closed] android android

Random element from string array [closed]


Just store the index generated in a variable, and then access the array using this varaible:

int idx = new Random().nextInt(fruits.length);String random = (fruits[idx]);

P.S. I usually don't like generating new Random object per randoization - I prefer using a single Random in the program - and re-use it. It allows me to easily reproduce a problematic sequence if I later find any bug in the program.

According to this approach, I will have some variable Random r somewhere, and I will just use:

int idx = r.nextInt(fruits.length)

However, your approach is OK as well, but you might have hard time reproducing a specific sequence if you need to later on.