BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Math.random() Method

By Chaitanya Singh | Filed Under: java

Java Math.random() method returns a random double number between 0.0 and 1.0, where 0.0 is inclusive and 1.0 is exclusive. Number is chosen from this range randomly and each number has equal probability.

public class JavaExample
{
  public static void main(String[] args)
  {
    System.out.println(Math.random());
  }
}

Output: You will most likely get a different output when you run this program.

0.4916566712907685

Syntax of Math.random() method

Math.random(); //returns a random number between 0.0 and 1.0

random() Description

public static double random(): It returns a double number which is greater than equal to 0.0 and less than 1.0.

random() Parameters

  • NA

random() Return Value

  • Pseudorandom double number from the range of 0.0 and 1.0.

Example 1: Random number between 1 and 100

public class JavaExample
{
  public static void main(String[] args)
  {
    double rand = Math.random()*100;
    System.out.println(rand);
  }
}

Output:

Java Math.random() Example Output1

Example 2: Generate negative random number

public class JavaExample
{
  public static void main(String[] args)
  {
    double rand = Math.random()*(-10);
    System.out.println(rand);
  }
}

Output:

Java Math.random() Example Output2

Example 3: Generate random number with no upper limit

public class JavaExample
{
  public static void main(String[] args)
  {
    double rand = Math.random()*(Double.MAX_VALUE);
    System.out.println(rand);
  }
}

Output:

Java Math.random() Example Output3

Recommended Posts

  • Java Math.floorDiv()
  • Java Math.floor()
  • Java Math.expm1()
  • Java Math.toRadians()
❮ Java Math

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap