Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Generate random numbers using Javafaker API – Java

Posted on 02/19/2025 By admin

We may require to generate random numbers in programming languages for multiple purposes like test data creation or numeric IDs for employees etc. We have multiple libraries available to generate random numbers in Java but I personally feel Javafaker APIs are easier to use compared to others.

We will learn to generate random whole numbers, decimal numbers, and also within a range in this post.

I have covered an introductory post on Javafaker API here.

I have used below Javafaker API dependency:-

  com.github.javafaker javafaker 1.0.2

Faker class in Javafaker library provides utility methods for generating fake strings, such as names, phone numbers, addresses. generate random strings with given patterns.

package FakerClassExamples;

import com.github.javafaker.Faker;

public class RandomNumberExample {
        
        public static void main(String[] args) {
                
                Faker faker = new Faker();
                // Generate random digit between 0 - 9 both inclusive
                System.out.println(faker.number().digit());
                System.out.println(faker.number().randomDigit());
                
                // Generate random digit between 1 - 9 both inclusive
                System.out.println(faker.number().randomDigitNotZero());
                
                // Generate digit of specific length
                System.out.println(faker.number().digits(3));
                System.out.println(faker.number().digits(5));
        }

}
5
3
6
608
72863

Generating a random number within a range is required at many times and I think Javafaker provides the easiest way to do so.

package FakerClassExamples;

import com.github.javafaker.Faker;

public class RandomNumberWithinRange {
        
        public static void main(String[] args) {
                
                Faker faker = new Faker();
                // Generate a number between 1(inclusive) to 999 (exclusive)
                System.out.println(faker.number().numberBetween(1, 999));
                
                // Generate a decimal random number within range
                System.out.println(faker.number().randomDouble(4, 10, 100));
        }

}
726
89.193

There are many useful methods provided by the Javafaker library which you can explore and learn about.

You can clone above Git Repo from here.

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: An Introduction to Selenium – A set of Tools
Next Post: Part 3: Usages Of Javascripts In Selenium: Problem You Might Face While Executing Javascript Commands In Selenium

Related Posts

Postman Tutorial Part 4 – Sending GET Request With Query Params in Postman Uncategorized
image – Make Selenium Easy Uncategorized
textbox – Make Selenium Easy Uncategorized
REST Assured Tutorial 34 – Serialization – Java Object To JSON Object Using Gson API Uncategorized
promptPopup – Make Selenium Easy Uncategorized
refresh token Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark