Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs
    • SQL FAQ’s

Java Random Number Generator

by suresh

The Java random function is one of the Java Math Library functions used to generate and return the Pseudo-random numbers between Zero and One.

The basic syntax of the Math random in Java Programming language is as shown below.

static double random(); //Return Type is Double

// In order to use in program: 
Math.random();

Java Random Number Generator example

The Java Math.random Function returns the Pseudo-random numbers between 0 to 1.

In this Java program, We are going to generate the random numbers in Java, and display the output.

TIP: The value generated by the Java random function is from 0 (included) and less than 1.

// Java Random Number Generator (Math.random) example

package MathFunctions;

public class RandomMethod {
	public static void main(String[] args) {
		
		System.out.println("\nRandom Number is = " + Math.random());
		System.out.println("\nRandom Numbers is = " + Math.random());

		System.out.println("\nRandom Numbers is = " + Math.random());
		System.out.println("\nRandom Numbers is = " + Math.random());
	}
}

OUTPUT

Java random Number Generator example 1

NOTE: If you observe the above screenshot, We called the Java Math.random Function four times, and it is returning four different (random) values.

Java Random Number Generator example 2

In this Java program, we will show how to store random values into an array. Here, we are going to declare an array of double types and fill that array with random valued generated by Java Math.random.

package MathFunctions;

public class RandomMethodInArray {
	public static void main(String[] args) {
		
		double [] myArray = new double[10];

		for (int i = 0; i < 10; i++) {
			myArray[i] = Math.random();
		}
		//Displaying Result
		for (int i = 0; i < myArray.length; i++) {
			System.out.println("Array Element = " + myArray[i]);
		}
	}
}

OUTPUT

Java random Number Generator example 2

ANALYSIS

First, We declared an empty double Array.

double [] myArray = new double[10];

Next, We used the Java For Loop to iterate the Array. Within the For Loop, we initialized the i value as 0. Next, the compiler will check for the condition (i < 10).

for (int i = 0; i < 10; i++) {

The following Java Random statements will store the random values into an array. If you observe the code snippet, we are assigning each index position with one random value.

Here, the compiler will call the random Math function (static double random() ) to return a random value between 0 and 1.

myArray[i] = Math.random();

Next, to display the array values We used another Java For Loop to iterate the Array. Within the For Loop, we initialized i value as 0, and the compiler will check the condition (i < myArray.length).

TIP: myArray.length finds the length of an array.

for (int i = 0; i < myArray.length; i++) {

The following Java statements prints the output.

System.out.println("Array Element = " + myArray[i]);

Placed Under: Java

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy