Java Decimal Format randomDouble(double minDouble, double maxDouble)

Here you can find the source of randomDouble(double minDouble, double maxDouble)

Description

random Double

License

Apache License

Declaration

public static double randomDouble(double minDouble, double maxDouble) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.DecimalFormat;

import java.util.Random;

public class Main {
    private static final ThreadLocal<Random> localRandom = new ThreadLocal<Random>() {
        @Override/*from   w  w  w .java2  s  . c  o  m*/
        protected Random initialValue() {
            return new Random();
        }
    };

    public static double randomDouble(double minDouble, double maxDouble) {
        return minDouble == maxDouble ? minDouble
                : randomInt((int) getCorrectionValue(minDouble * 100000, 0),
                        (int) getCorrectionValue(maxDouble * 100000, 0)) / 100000.0;
    }

    public static double randomDouble() {
        return localRandom.get().nextDouble();
    }

    public static int randomInt(int minInt, int maxInt) {
        return minInt == maxInt ? minInt
                : (Math.min(minInt, maxInt) + localRandom.get().nextInt(Math.abs(maxInt - minInt) + 1));
    }

    public static double getCorrectionValue(double basicValue, int digit) {
        if (digit < 0)
            return basicValue;
        StringBuilder sb = new StringBuilder("#");
        if (digit > 0) {
            sb.append("0.");
            for (int i = 0; i < digit; i++) {
                sb.append("0");
            }
        }
        DecimalFormat format = new DecimalFormat(sb.toString());
        return Double.parseDouble(format.format(basicValue));
    }

    public static Number min(Number... numbers) {
        Number min = Double.MAX_VALUE;
        for (Number number : numbers) {
            if (number.doubleValue() < min.doubleValue()) {
                min = number;
            }
        }
        return min;
    }

    public static int nextInt() {
        return localRandom.get().nextInt();
    }

    public static int nextInt(int n) {
        return localRandom.get().nextInt(n);
    }
}

Related

  1. printAlpha(double a[])
  2. printAngle(double angle)
  3. printArray(String title, double[] vect)
  4. printArrayP(double[][] p)
  5. PrintWith1DecAnd000Sep(double Number)
  6. randomGenLat(Double latstart, Double latend)
  7. randomGenLngLat(Double lngstart, Double lngend, Double latstart, Double latend)
  8. readThisDouble(final String value)
  9. renderDouble(double value, int precision)