Java Random Byte Array randomBytes(Random r, int length)

Here you can find the source of randomBytes(Random r, int length)

Description

For those that like the random bytes to be created and returned.

License

Open Source License

Parameter

Parameter Description
r to keep the same randomizer just pass it in..
length size of the array of random data returned.

Return

byte array of random data.

Declaration

public static byte[] randomBytes(Random r, int length) 

Method Source Code


//package com.java2s;
/*//from w  ww .  ja v a2  s.c o m
 * ====================
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Common Development
 * and Distribution License("CDDL") (the "License").  You may not use this file
 * except in compliance with the License.
 *
 * You can obtain a copy of the License at
 * http://opensource.org/licenses/cddl1.php
 * See the License for the specific language governing permissions and limitations
 * under the License.
 *
 * When distributing the Covered Code, include this CDDL Header Notice in each file
 * and include the License file at http://opensource.org/licenses/cddl1.php.
 * If applicable, add the following below this CDDL Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 * ====================
 */

import java.util.Random;

public class Main {
    /**
     * For those that like the random bytes to be created and returned. Uses the
     * {@link Random#nextBytes(byte[])} method to generate the random data.
     *
     * @param r
     *            to keep the same randomizer just pass it in..
     * @param length
     *            size of the array of random data returned.
     * @return byte array of random data.
     */
    public static byte[] randomBytes(Random r, int length) {
        byte[] ret = new byte[length];
        r.nextBytes(ret);
        return ret;
    }

    /**
     * Get a random array of bytes with the length specified.
     *
     * @param length
     *            the size of the byte array returned.
     * @return random array of bytes with the length specified.
     */
    public static byte[] randomBytes(int length) {
        return randomBytes(new Random(), length);
    }

    /**
     * Random array of bytes with a random length. The length should be no
     * greater that 4k.
     *
     * @param r
     *            uses the randomizer provided to generate the random data.
     * @return a byte array no bigger than 4k filled with random data.
     */
    public static byte[] randomBytes(Random r) {
        return randomBytes(r, r.nextInt(4048));
    }

    /**
     * Random array of bytes with a random length. The length should be no
     * greater that 4k.
     *
     * @return a byte array no bigger than 4k filled with random data.
     */
    public static byte[] randomBytes() {
        return randomBytes(new Random());
    }
}

Related

  1. randomBytes(int length)
  2. randomBytes(int length)
  3. randomBytes(int minimumCharacters, int maximumCharacters)
  4. randomBytes(int size)
  5. randomBytes(long seed, int blockCount, int blockSize)
  6. randomBytes(Random rand, int size)
  7. randomBytesSlowNextInt(Random r, byte[] buf, int from, int len)
  8. randomBytesWithEveryPossibleByteValueRepresentedAtLeastOnce()