Java tutorial
//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 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; } } }