Generate a random double in a range

Viewed 379063

I have two doubles like the following

double min = 100;
double max = 101;

and with a random generator, I need to create a double value between the range of min and max.

Random r = new Random();
r.nextDouble();

but there is nothing here where we can specify the range.

7 Answers
import java.util.Random;

public class MyClass {
     public static void main(String args[]) {
          Double min = 0.0;  // Set To Your Desired Min Value
          Double max = 10.0; // Set To Your Desired Max Value
          double x = (Math.random() * ((max - min) + 1)) + min;   // This Will Create A Random Number Inbetween Your Min And Max.
          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.
          System.out.println(xrounded);    // This Will Now Print Out The Rounded, Random Number.
     }
}

Hope, this might help the best : Random Number Generators in Java


Sharing a Complete Program:

import java.util.Random;

public class SecondSplitExample
{
    public static void main(String []arguments)
    {
        int minValue = 20, maxValue=20000;
        Random theRandom = new Random();
        double theRandomValue = 0.0;
        
        // Checking for a valid range-
        if( Double.valueOf(maxValue - minValue).isInfinite() == false ) 
            theRandomValue = minValue + (maxValue - minValue) * theRandom.nextDouble();
        
        System.out.println("Double Random Number between ("+ minValue +","+ maxValue +") = "+ theRandomValue);
    }
}

Here is the output of 3 runs:

Code>java SecondSplitExample
Double Random Number between (20,20000) = 2808.2426532469476

Code>java SecondSplitExample
Double Random Number between (20,20000) = 1929.557668284786

Code>java SecondSplitExample
Double Random Number between (20,20000) = 13254.575289900251

Learn More:

Random random = new Random();
double percent = 10.0; //10.0%
if (random.nextDouble() * 100D < percent) {
    //do
}

The main idea of random is that it returns a pseudorandom value. There is no such thing as fully random functions, hence, 2 Random instances using the same seed will return the same value in certain conditions.

It is a good practice to first view the function doc in order to understand it (https://docs.oracle.com/javase/8/docs/api/java/util/Random.html)

Now that we understand that the returned value of the function nextDouble() is a pseudorandom value between 0.0 and 1.0 we can use it to our advantage.

For creating a random number between A and B givin' that the boundaries are valid (A>B) we need to: 1. find the range between A and B so we can know how to many "steps" we have. 2. use the random function to determine how many steps to take (because the returned value is between 0.0 and 1.0 you can think of it as "pick a random percentage of increase" 3. add the offset

After all of that, you can see that mob gave you the easiest and most common way to do so in my opinion

double randomValue = rangeMin + (rangeMax - rangeMin) * r.nextDouble();

double RandomValue = Offset + (Range)*(randomVal between 0.0-1.0)

Related