How to Generate Array Of Random Numbers In Java

In this instructional exercise, we will learn how to generate the array of random numbers in Java using simple java code.
If you don’t know how to generate random numbers, then you are in the right place to search for your solution.

If you can do this, then you will also be interested in making Guess The Number Game Using Java with Source Code.

Generate Array Of Random Numbers In Java

Let’s understand first why we need to code to find the random numbers, because-

Irregular number of generators are valuable for a wide range of purposes. Beside clear applications like producing irregular numbers for the motivations behind betting or making eccentric outcomes in a PC game, randomness is significant for cryptography. Cryptography requires numbers that aggressors or attackers can’t figure.

Simple code to find the array of random numbers is shown below:-

import java.util.*;
import java.util.Scanner;
class RandomNumbersExample {
public static void main(String[] args)
 {
    int number;
    System.out.println("Example to find the array of random numbers");
    System.out.println("Enter the range upto where you need to get the random numbers:");
    Scanner sc= new Scanner(System.in);
    number=sc.nextInt();
    Random rand = new Random();
    System.out.println("*****OUTPUT*****");
    System.out.println("Random numbers are:-");
    for (number = 1; number <= 10; number++) {
      System.out.println(rand.nextInt(number));
    }
  }
}

 

The output of the above code is-

Example to find the array of random numbers
Enter the range upto where you need to get the random numbers:
100
*****OUTPUT*****
Random numbers are:-
35
87
0
85
96
82
10
93
54
4

You may also read:

Leave a Reply

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