Java Random Int randomBytes(int size)

Here you can find the source of randomBytes(int size)

Description

random Bytes

License

Open Source License

Declaration

public static final byte[] randomBytes(int size) 

Method Source Code

//package com.java2s;

public class Main {
    private static final byte[] bytes = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'q', 'w', 'e', 'r', 't',
            'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm',
            'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X',
            'C', 'V', 'B', 'N', 'M' };
    private static final long multiplier = 0x5DEECE66DL;
    private static final long addend = 0xBL;
    private static final long mask = (1L << 48) - 1;
    private static final long integerMask = (1L << 33) - 1;
    private static long seed;

    public static final byte[] randomBytes(int size) {
        byte[] bb = bytes;
        byte[] ab = new byte[size];
        for (int i = 0; i < size; i++) {
            ab[i] = randomByte(bb);/*from ww w . ja v a  2 s  . com*/
        }
        return ab;
    }

    private static byte randomByte(byte[] b) {
        int ran = (int) ((next() & integerMask) >>> 16);
        return b[ran % b.length];
    }

    private static long next() {
        long oldSeed = seed;
        long nextSeed = 0L;
        do {
            nextSeed = (oldSeed * multiplier + addend) & mask;
        } while (oldSeed == nextSeed);
        seed = nextSeed;
        return nextSeed;
    }
}

Related

  1. randomBlock(byte[] block, int off, int len)
  2. randomByteArray(int length)
  3. randomByteArray(int size, byte from, byte to)
  4. randomByteAsInt()
  5. randomBytes(int size)
  6. randomCommon(int min, int max, int n)
  7. randomCommonStr(int min, int max, int n)
  8. randomDoubleMatrix(int rows, int columns)
  9. randomFloatInInterval(float lower, float upper)