Java Program to Print Random Character of String

Published by user on

In this post, we will look at how to print random characters from a String in Java.


  • Generate a random index using the Math.random()
  • Convert index to int
  • Find the character at that index from the String and print it


Let’s have a look at the program.

// Java program to
// demonstrate how to find
// random character in a String
public class RandomCharacter {

	public static void main(String[] args) {

		String str = "StringToFindCharacter";

		// generate random index
		int index = (int) (Math.random() * (str.length() - 1));

		// get the character at that index
		char ch = str.charAt(index);

		System.out.println("Random Character from " + str + " at index (" + index + ") is " + ch);


Random Character from StringToFindCharacter at index (5) is g
Categories: Java