# 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));`.

### 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 …

### Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …

### Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …

### Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …

### Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …