All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Generate Random Numbers In Java Within Range

Last Updated : Mar 11, 2024

How To Generate Random Numbers In Java Within Range

In this article we will show you the solution of how to generate random numbers in java within range, as usual you need to import package, in case you using eclipse then each program have itself. The random package is must for this program to achieve the goal.

Within main method we provide example, there we generating random number between 0, 20 whole digit. Then printed result on terminal by println() method.

Step By Step Guide On How To Generate Random Numbers In Java Within Range :-

In main class ‘Randnum_prgm’, we provide definion for public static main method. To generate random number we need random package support to use random object.

With the help of random object now you can generate random number between given range.

You can set as your limit by own in our example we using 20 as end digit. So it will generate 0,20 whole digit integer and displayed result will print on terminal.

//Random Number Generation
package java_prgms;
import java.util.Random;
class Randnum_prgm
 public static void main(String args[])
        Random rdm= new Random();
        int rndmNUM=rdm.nextInt(20);
        System.out.println("Random number will display between rang 0 to 19 : "+rndmNUM );
  1. The ‘Package java_prgms’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces.
  2. Here we provide two packages, the first package will insert by eclipse to support java codes then the ‘Random’ specifically allow to create random object without error.
  3. In main class ‘Randnum’, we defined main method. There we creating Random object ‘rdm’ like other object creation syntax.
  4. To generate random digit you have to appends that object with ‘nextInt’ with limit ‘20’. That will generates the random integer value and stored on variable ‘rndmNUM’. You can change the 20 value with any other as per your wish. The 20 is denotes end limit number or upper limit.
  5. Using println() method we displaying result with string ‘Random number will display between rang 0 to 19 :’ and result value.
  6. The ‘System.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  7. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion now we are able to know how to generate random integer type number in java.

To compile java program you need to tell the java compiler which program to compile through ‘javac’.

After successful compilation you need to execute ‘java Randnum_prgm‘ line for gets the output.

When you run the program by simply clicks on run button on eclipse will display output ‘Random number will display between rang 0 to 19 : 15’.

I hope this article on how to generate random numbers in java within range helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪