how to use random bound on doubles java

Solutions on MaxInterview for how to use random bound on doubles java by the best coders in the world

showing results for - "how to use random bound on doubles java"
Erine
10 Jun 2020
1import java.util.Random;
2
3public class MyClass {
4     public static void main(String args[]) {
5          Double min = 0.0;  // Set To Your Desired Min Value
6          Double max = 10.0; // Set To Your Desired Max Value
7          double x = (Math.random() * ((max - min) + 1)) + min;   // This Will Create A Random Number Inbetween Your Min And Max.
8          double xrounded = Math.round(x * 100.0) / 100.0;   // Creates Answer To The Nearest 100 th, You Can Modify This To Change How It Rounds.
9          System.out.println(xrounded);    // This Will Now Print Out The Rounded, Random Number.
10     }
11}