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 {
    /**
     * Fills the array with random ints.  Values will be between min (inclusive) and
     * max (inclusive).
     */
    public static void genRandomInts(long seed, int min, int max, int array[]) {
        Random r = new Random(seed);
        for (int i = 0; i < array.length; i++) {
            long range = max - min + 1;
            array[i] = (int) (min + r.nextLong() % range);
        }
        array[r.nextInt(array.length)] = min;
        array[r.nextInt(array.length)] = max;
    }

    public static void genRandomInts(long seed, int array[], boolean signed, int numberOfBits) {
        long[] longs = new long[array.length];
        genRandomLongs(seed, longs, signed, numberOfBits);
        for (int i = 0; i < array.length; i++) {
            array[i] = (int) longs[i];
        }
    }

    /**
     * Fills the array with random longs.  If signed is true, negative values can be generated.
     * The values will fit within 'numberOfBits'.  This is useful for conversion tests.
     */
    public static void genRandomLongs(long seed, long array[], boolean signed, int numberOfBits) {
        long positiveMask = numberOfBits == 64 ? -1 : ((1l << numberOfBits) - 1);
        long negativeMask = ~positiveMask;
        Random r = new Random(seed);
        for (int i = 0; i < array.length; i++) {
            long l = r.nextLong();
            if (signed && l < 0) {
                l = l | negativeMask;
            } else {
                l = l & positiveMask;
            }
            array[i] = l;
        }
        // Seed a few special numbers we want to be sure to test.
        array[r.nextInt(array.length)] = 0l;
        array[r.nextInt(array.length)] = 1l;
        array[r.nextInt(array.length)] = positiveMask;
        if (signed) {
            array[r.nextInt(array.length)] = negativeMask;
            array[r.nextInt(array.length)] = -1;
        }
    }
}