Java Program to Generate Random Numbers3 min read

Generating Random numbers in java is quite easy as it provides various classes to do that. It comes in handy when needed to apply to any kind of application development that might require random number generation. Let start by knowing random numbers.

What is Randon Number?

We can define Random as something that happens without any conscious decision. Similarly, a random number is a number that is chosen unpredictably or randomly from the set of numbers.

Random Number can be generated using two ways.

  • java.util.Random class
  • Math.random() method

We will also learn to generate between the range, at last.


Java Program to generate Random Number

1. Using java.util.Random class:

java.util.Random class allows us to generate random numbers of type integers, doubles, floats, longs, and booleans. In the example below, we will see to generate numbers of type Integers and booleans. Then you can apply to other data-types.

Program:

The output of generating random numbers using java.util.Random class:


2. Using Math.random() method:

Math.random() method allows you to create only a random number of type doubles as shown below.

Program:

The output of generating random numbers using Math.random() method:


Java Program to generate Random Number within the range

The output to generate random numbers between the range in java:

How many random Numbers do you want?
4
What is the last range of number starting from 0 to _?
30

4 Random integers within 0 to 30 are :
1
15
9
28
4

This random generation of numbers within range can be applied to Math.random() method, we only need to replace the code inside the for loop to System.out.println((int)(Math.random() * lastNum));.


MORE

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …