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 floats.  Values will be between min (inclusive) and
     * max (inclusive).
     */
    public static void genRandomFloats(long seed, float min, float max, float 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++) {
                float mantissa = r.nextFloat();
                int exponent = minExponent + r.nextInt(maxExponent - minExponent);
                int sign = (min >= 0) ? 1 : 1 - r.nextInt(2) * 2; // -1 or 1
                float rand = sign * mantissa * (float) 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++) {
                float rand = r.nextFloat();
                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++) {
            float f = (float) sInterestingDoubles[i];
            if (min <= f && f <= max) {
                array[r.nextInt(array.length)] = f;
            }
        }
        array[r.nextInt(array.length)] = min;
        array[r.nextInt(array.length)] = max;
        if (includeExtremes) {
            array[r.nextInt(array.length)] = Float.NaN;
            array[r.nextInt(array.length)] = Float.POSITIVE_INFINITY;
            array[r.nextInt(array.length)] = Float.NEGATIVE_INFINITY;
            array[r.nextInt(array.length)] = Float.MIN_VALUE;
            array[r.nextInt(array.length)] = Float.MIN_NORMAL;
            array[r.nextInt(array.length)] = Float.MAX_VALUE;
            array[r.nextInt(array.length)] = -Float.MIN_VALUE;
            array[r.nextInt(array.length)] = -Float.MIN_NORMAL;
            array[r.nextInt(array.length)] = -Float.MAX_VALUE;
        }
    }
}