All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Generate 10 Random Numbers In Java

Last Updated : Mar 11, 2024

How To Generate 10 Random Numbers In Java

In this article we will show you the solution of how to generate 10 random numbers in java, as usual first package is must, in case you are using eclipse then each program import itself during creation of project.

Here one more package you need so we imported random package to achieve the goal. Inside main method we provide definition same as random number generation but the difference is for loop.

If you want more numbers at a time everyone have to go with loop format.

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

In main class ‘Rnum_prgm’, we provide definition for public static main method.

Whenever you need to generate random number you have to create random object as foremost in program.

To get whole support that imported random package helps you, so might ensure whether you are defined or not. Then you need to create random object and bind it with nextInt().

Here you can pass any values as limit for random integer generation as per your wish then to get 10 times of random generation we inserting this main line inside for loop with 10 limit.

//10 Random Number Generation
package java_prgms;
import java.util.Random;
class Rnum_prgm
{
 public static void main(String args[])
    {
  System.out.println("Generated 10 Random Numbers");
  Random rand=new Random();
  for(int i=0;i<=9;i++){
   System.out.println(rand.nextInt(11));
  }
     }
}
  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 you will see two packages, the first package inserted by eclipse to support java codes then the ‘Random’ specifically allow you to create random object and get result without error.
  3. We defined main class ‘Rnum_prgm’, with main method definition. First we displayed ‘Generated 10 Random Numbers’ text of program concept to instruct users that provides clarity to end users.
  4. Then we creating Random object ‘rand’, ensure in this line of object creation you have to pay some attention to detail.
  5. For loop() definition provide with iteration value ‘i’. To continue loop 10 times we sets initial value ‘0’ and condition up to ‘9’. You can also set initial value ‘1’ and condition till 10 both will loop 10 times without fail.
  6. Inside for loop we printing random numbers successfully by appending ‘nextInt()’ with random object ‘rand’. The nextInt() help you to generate integer data type numbers and value ‘11’ refers upper limit of random generation process, you can modify it if need.
  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 10 random numbers in java.

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

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

When you run the program by simply clicks on run button on eclipse will display output ‘Generated 10 Random Numbers’ 10, 7, 3, 2, 2, 5, 7, 1, 4, 9.

You can use any other loop than for that not affect yours result and output might vary because its random generation.

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