Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Random;

public class Main {
    private static final double[] sInterestingDoubles = { 0.0, 1.0, Math.E, Math.PI, Math.PI / 2.0, Math.PI * 2.0,
            -0.0, -1.0, -Math.E, -Math.PI, -Math.PI / 2.0, -Math.PI * 2.0, };

    /**
     * Fills the array with random doubles.  Values will be between min (inclusive) and
     * max (inclusive).
     */
    public static void genRandomDoubles(long seed, double min, double max, double array[],
            boolean includeExtremes) {
        Random r = new Random(seed);
        int minExponent = Math.min(Math.getExponent(min), 0);
        int maxExponent = Math.max(Math.getExponent(max), 0);
        if (minExponent < -6 || maxExponent > 6) {
            // Use an exponential distribution
            int exponentDiff = maxExponent - minExponent;
            for (int i = 0; i < array.length; i++) {
                double mantissa = r.nextDouble();
                int exponent = minExponent + r.nextInt(maxExponent - minExponent);
                int sign = (min >= 0) ? 1 : 1 - r.nextInt(2) * 2; // -1 or 1
                double rand = sign * mantissa * Math.pow(2.0, exponent);
                if (rand < min || rand > max) {
                    continue;
                }
                array[i] = rand;
            }
        } else {
            // Use a linear distribution
            for (int i = 0; i < array.length; i++) {
                double rand = r.nextDouble();
                array[i] = min + rand * (max - min);
            }
        }
        // Seed a few special numbers we want to be sure to test.
        for (int i = 0; i < sInterestingDoubles.length; i++) {
            double d = sInterestingDoubles[i];
            if (min <= d && d <= max) {
                array[r.nextInt(array.length)] = d;
            }
        }
        array[r.nextInt(array.length)] = min;
        array[r.nextInt(array.length)] = max;
        if (includeExtremes) {
            array[r.nextInt(array.length)] = Double.NaN;
            array[r.nextInt(array.length)] = Double.POSITIVE_INFINITY;
            array[r.nextInt(array.length)] = Double.NEGATIVE_INFINITY;
            array[r.nextInt(array.length)] = Double.MIN_VALUE;
            array[r.nextInt(array.length)] = Double.MIN_NORMAL;
            array[r.nextInt(array.length)] = Double.MAX_VALUE;
            array[r.nextInt(array.length)] = -Double.MIN_VALUE;
            array[r.nextInt(array.length)] = -Double.MIN_NORMAL;
            array[r.nextInt(array.length)] = -Double.MAX_VALUE;
        }
    }
}