Java Random Double randDouble(int m, int n)

Here you can find the source of randDouble(int m, int n)

Description

rand Double

License

Open Source License

Parameter

Parameter Description
m a parameter
n a parameter

Return

An Unsigned numeric value with m total digits, of which n digits are to the right(after) the decimal point.

Declaration

public static double randDouble(int m, int n) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Random;

public class Main {
    private static final Random seed = new Random();

    /**//from  ww w. ja  va2 s .  c  om
     * 
     * @param m
     * @param n
     * @return An Unsigned numeric value with m total digits, of which n digits
     *         are to the right(after) the decimal point.
     */
    public static double randDouble(int m, int n) {
        assert (m >= n);
        int left = m - n;
        return Double.parseDouble(randNString(left, left) + "." + randNString(n, n));
    }

    /**
     * @returns a random numeric string with length in range [minimum_length,
     *          maximum_length].
     */
    public static String randNString(int minimum_length, int maximum_length) {
        return randomString(minimum_length, maximum_length, '0', 10);
    }

    private static String randomString(int minimum_length, int maximum_length, char base, int numCharacters) {
        int length = randLong(minimum_length, maximum_length).intValue();
        byte baseByte = (byte) base;
        byte[] bytes = new byte[length];
        for (int i = 0; i < length; ++i) {
            bytes[i] = (byte) (baseByte + randLong(0, numCharacters - 1));
        }
        return new String(bytes);
    }

    public static Long randLong(long minimum, long maximum) {
        assert minimum <= maximum;
        long value = Math.abs(seed.nextLong()) % (maximum - minimum + 1) + minimum;
        assert minimum <= value && value <= maximum;
        return value;
    }
}

Related

  1. randDouble()
  2. randDouble(double min, double max)
  3. randDouble(double min, double max)
  4. randDouble(double min, double max)
  5. randDouble(double min, double max)
  6. randIn(double min, double max)
  7. RandInRange(double start, double end)
  8. random(double low, double high)
  9. random(double min, double max)