All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Java Generate Random Number Between 1 And 100

Last Updated : Mar 11, 2024

Java Generate Random Number Between 1 And 100

In this article we will show you the solution of java generate random number between 1 and 100, first you need to import package, in case you using eclipse then each program have itself.

Then imported random package will help you to achieve the goal. In main method we provide example, there we generating random number between 1 to 100 whole digits.

Step By Step Guide On Java Generate Random Number Between 1 And 100 :-

In the main class ‘Rand_prgm’, we provide definion for public static void main method.

As we seen on earlier tutorial, the random package will support you to define random object. With the help of random object only you can generate random number From 1 to 100 range.

Using random object creating random variable 'rdn'. Where the creation you have to set your maximum count, so we given 100.

//Generate Random Number 1 to 100
package java_prgms;
import java.util.Random;
class Rand_prgm
{
 public static void main(String args[])
    {
        Random r= new Random();
        int rdn=r.nextInt(100);
        rdn=rdn+1;
        System.out.println("Number will display between 1 to 100 : "+rdn);
    }
}
  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. Note you have to import random package to achieve the result. Then the first package will insert by eclipse to support java codes. Without them you will get error at terminal.
  3. In main class ‘Rand’, we provide definition of main method. Within main method firstly we defined random object 'r' to generate random number.
  4. Now you need to appends random object with 'nextInt' that will help you to create integer data type number. Then within bracket 100 denotes the maximum limit for random number generation.
  5. Generated random integer stored on variable 'rdn'. Then in the given limit it will not considers last digit for that reason next step we adding 1 with variable 'r'.
  6. Finally we printed result at terminal using println() method.
  7. 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.
  8. 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 number between 1 to 100 range in java.

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

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

To verify or get the output click on run button on eclipse will display output ‘Number will display between 1 to 100 : 65’.

I hope this article on java generate random number between 1 and 100 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 🡪