ExamTray App is now Available on Google Play EXAMTRAY android app link

 

Java Program: How to Print a Random Element or Index of An Array, ArrayList

It is programmers need to choose or select or get a random element or random index of an Array or ArrayList in Java. The random element may be a number or string. Let us explore Math.random() method with examples. The same code can be used to implement a Lottery Draw to pick a random contestant from a list of participants.

Core Java Library java.lang.Math provides many ready-made methods to achieve many functionalities. It is part of the java.base module. All java.lang package classes are imported automatically by the compiler. There is no need to explicitly import the class. These classes and methods allow us to generate random numbers easily.

A short note on java.Math.random() method

Math is a java class. random() is a static method in Math class. It returns a random double value between 0.0 and 1.0. It will be less than 1 always.

Syntax:

public static double random(){ }

Example: Print a random number between 0 and 10

Remember to typecast the double result to an integer or int type.

int randomNumber= (int)(Math.random()*11);
System.out.println("Random Number = " + randomNumber);
//output is in between 0 and 10.

Print or Select a Random Element or Index of an Array

To get a random number within the limit of Array Index, we have to multiply the random number generated by Math.random() with the (ArrayLength) and typecast to an integer. This gives us a random Array Index at any point of time and every time. If we print the Array Element with this random index, what we get is a random element of the said Array. It is more or like picking a random string from a group of strings.

String days[] = {"SUNDAY","MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY","FRIDAY", "SATURDAY"};

int randomIndex = (int)(Math.random()*days.length);
System.out.println("Random Index = " + randomIndex);

String randomElement = days[randomIndex];
System.out.println("Random Element = " + randomElement);

Print or Select a Random Element or Index of an ArrayList

To get a random number within the limit of ArrayList Size, we have to multiply the random number generated by Math.random() with the (ArrayListSize) and typecast to an integer. This gives us a random ArrayList Index at any point of time and every time. Using the index, we can print both random index and random ArrayList element easily.

ArrayList list = new ArrayList();
list.add("SUNDAY");list.add("Monday");
list.add("TUESDAY");list.add("WEDNESDAY");

int randomIndex = (int)(Math.random()*list.size());
System.out.println("Random Index = " + randomIndex);

String randomElement = list.get(randomIndex);
System.out.println("Random Element = " + randomElement);

Using Another Random Number Generator Class: java.util.Random

Java module java.base includes another version of Random class inside java.util Package. This version of random number is in integer form only. So, there is no need to typecast as we did for Math.random() output. Here we extensively use nextInt(size) method. The method nextInt() returns a number between 0(including) and SIZE(excluding).

import java.util.Random;
public class MyClass
{
  public static void main(String[] args)
  {
    int ary[] = {10,11,12,13,14,15};
    Random ran = new Random();
    int randomNumber = ran.nextInt(ary.length);
    int randomArrayElement = ary[randomNumber];
  }
}

Last Minute Java Tutorial is already available on examtray.com. Improve your Java skills by going through the lessons on topic wise.

This is how we print or select or get a random element from an Array or ArrayList in Java. Share this article with your friends and colleagues to encourage authors.