Java Math random example

In this post, we will see about Java math random example.
Math’s random method provides you positive signed double value from 0.0 to 1.0.

Syntax

This method can be used in multithreaded environment as it is thread safe.
If you see the source code for random math, you will see that it uses java.util.Random class’s nextDouble() method to generate random number between 0.0 to 1.0.
Source code of Math’s random() method
So you can see from above code, random methods uses java.util.Random class to generate random number.

Example

Let’s say you want to generate random integer from 1 to 10. You can use Math’s random method as below:

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

Pass maximum as 10 and minimum as 1.You will get random integer between 1 to 10.
Here is the complete example:

When you run above program, you will get below output:

Math’s random method output
0.6094762145403727 0.984629643759588 0.1391747855327513 0.03367790074630195 0.7327997405792047 0.29391413463517546 0.2757890922600933 0.19606812863092626 0.8941801486372356
Generating random number from 1 to 10
6 8 0 5 8 5 5 0 3

You can use java.util.Random class’s nextInt method also to get random integer values from 0 to 10.

When you run above program, you will get below output:

Generating random number using Random’s nextInt
9 7 4 3 7 1 7 2 5

random.nextInt(10) method will generate random integer between 0 to 10.
that’s all above Math’s Random method.

Related Posts

  • 28 September

    PI in Java

    The Pi is a constant value in Mathematics that represents a floating-point value 3.1415. It is mostly used in geometry to calculate area, circumference, volume, etc. If you have studied geometry in your academic, then you may be aware of use of Pi such as calculating area require this constant value. In Java, when we […]

  • 05 October

    Calculate total surface area of Cylinder in java

    In this post, we will see how to calculate total surface area of Cylinder in java. Formula of calculating total surface area of Cylinder is: Surface area of Cylinder = 2 *Π * r * r + 2 *Π * r * h Where r is radius of cylinder and h is height of cylinder […]

  • 05 October

    Calculate total surface area of Hemisphere in java

    In this post, we will see how to calculate total surface area of Hemisphere in java. Hemisphere is exactly half of sphere. 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our job portal. Looking for tech events? Go to tech events 🗓️ Calendar.️ There can be many practical examples of Hemisphere. You […]

  • 05 October

    Get random number between 0 and 1 in java

    In this post, we will see how to get random number between 0 to 1 in java. We have already seen random number generator in java. We can simply use Math.random() method to get random number between 0 to 1. Math.random method returns double value between o(inclusive) to 1(exclusive). [crayon-607a7fb484d4b411829677/] When you run above program, […]

  • 03 January

    java round double/float to 2 decimal places

    In this post, we will see how to round double to 2 decimal places in java. There are many ways to do it.Let’s go through few ways. Let’s understand each with the help of simple example. Using DecimalFormat You can use DecimalFormat too to round number to 2 decimal places. 💻 Awesome Tech Resources: Looking […]

  • 03 January

    How to get square root of number in java

    In this tutorial. we will see if how to get square root of number in java. It is very simple to get square root of number in java. You can simply use Math’s sqrt() method to calculate square root of number. Syntax [crayon-607a7fb4855fb456657695/] Return type It returns square root of the number. Square root of […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.