Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you generate a random number with decimal places

Tags:

java

I would like to generate random numbers between 1 & 10, with a decimal place of 1 
(e.g. 1.5, 9.4, 6.3, 2.9)

I would like to generate random numbers between 1 & 10, with 2 decimal places
(e.g. 1.51, 9.46, 6.32, 2.93)

I would like to generate random numbers between 1 & 10, with 3 decimal places

    (e.g. 1.512, 9.346, 6.329, 2.935)

One solution would be to have a random generator just for the decimal places and then concatenate with the number. Is there a simpler way to achieve this?

like image 411
user339108 Avatar asked Nov 10 '10 10:11

user339108


Video Answer


4 Answers

Although BigDecimal solutions are more flexible, here's a simpler solution using int and double. This method takes a Random object, an upper and lower bound and a number of decimal places and returns an accordingly formatted string:

/**
 * Generate a decimal string representation of a random number within the
 * supplied bounds.
 * 
 * @param random
 *            the random object (if null, a new one will be created)
 * @param lowerBound
 *            the lower bound, inclusive
 * @param upperBound
 *            the upper bound, inclusive
 * @param decimalPlaces
 *            the decimal places of the result
 * @return the formatted string
 */
public static String getRandomValue(final Random random,
    final int lowerBound,
    final int upperBound,
    final int decimalPlaces){

    if(lowerBound < 0 || upperBound <= lowerBound || decimalPlaces < 0){
        throw new IllegalArgumentException("Put error message here");
    }

    final double dbl =
        ((random == null ? new Random() : random).nextDouble() //
            * (upperBound - lowerBound))
            + lowerBound;
    return String.format("%." + decimalPlaces + "f", dbl);

}

Test Code:

final Random rnd = new Random();

for(int decpl = 0; decpl < 3; decpl++){
    for(int low = 0; low < 2; low++){
        for(int high = low + 1; high < low + 3; high++){
            System.out.println("Random Value between " + low + " and "
                + high + " with " + decpl + " decimal places:");
            System.out.println(getRandomValue(rnd, low, high, decpl));
        }
    }
}

Output:

Random Value between 0 and 1 with 0 decimal places:
0
Random Value between 0 and 2 with 0 decimal places:
1
Random Value between 1 and 2 with 0 decimal places:
1
Random Value between 1 and 3 with 0 decimal places:
3
Random Value between 0 and 1 with 1 decimal places:
0.6
Random Value between 0 and 2 with 1 decimal places:
1.5
Random Value between 1 and 2 with 1 decimal places:
1.1
Random Value between 1 and 3 with 1 decimal places:
1.9
Random Value between 0 and 1 with 2 decimal places:
0.52
Random Value between 0 and 2 with 2 decimal places:
0.82
Random Value between 1 and 2 with 2 decimal places:
1.75
Random Value between 1 and 3 with 2 decimal places:
1.10
like image 126
Sean Patrick Floyd Avatar answered Oct 19 '22 00:10

Sean Patrick Floyd


1)
(int) ((Math.random() * 90) + 10) / 10.0
2)
(int) ((Math.random() * 900) + 100) / 100.0
3)
(int) ((Math.random() * 9000) + 1000) / 1000.0

like image 36
christian Avatar answered Oct 19 '22 00:10

christian


Well one obvious suggestion is to generate a single integer and divide it appropriately. So for 3DPs, you'd generate a number between 1000 and 9999 (or 10000, depending on the exact bound you wanted) and then divide it by 10000.

I suggest you use BigDecimal in the actual division, to maintain the decimals exactly.

like image 37
Jon Skeet Avatar answered Oct 19 '22 01:10

Jon Skeet


Generate a double and than format according to your decimal need

like image 35
jmj Avatar answered Oct 19 '22 00:10

jmj