How to generate random number in java

By | August 9, 2020

How to generate random number in java:

In this tutorial, you are going to learn how to generate a random number in java. We are using randomNumeric method present in RandomStringUtils class of Apache to generate random number in java.

It creates a random string whose length is the number of characters specified. Characters will be chosen from the set of numeric characters.

Syntax:

RandomStringUtils.randomNumeric(int count)

Example:

Output: It prints a 5 digit number in the form of a string as we have given the length of random string to create as 5.

Ex: 77463

 

Generate a random string in java:

apache.commons.lang3.RandomStringUtils.randomAlphabetic:

Creates a random string whose length is the number of characters specified.

Characters will be chosen from the set of Latin alphabetic characters (a-z, A-Z).

Syntax:

RandomStringUtils.randomAlphabetic(int count)

Parameters:

  • count the length of random string to create

Returns:

  • the random string

Examples:

Output: It prints a random 5 char string for example WvnJP. Random characters are from a-z to A-Z.

 

Recommended Posts:

StringUtils methods in java

Tags: random string java

 

arrays tostring in java | selenium interview questions | binary search

 

 

 

 

 

Play & Win contest by Selenium99

Home 1

Leave a Reply

Your email address will not be published. Required fields are marked *