Java Random randomStr(Random rnd, int size)

Here you can find the source of randomStr(Random rnd, int size)

Description

random Str

License

Open Source License

Declaration

static byte[] randomStr(Random rnd, int size) 

Method Source Code

//package com.java2s;
/*//w  w w.  ja  v a2  s  .  co m
 *  Copyright (C) 2011-2017 Cojen.org
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.*;

public class Main {
    static byte[] randomStr(Random rnd, int size) {
        return randomStr(rnd, null, size, size);
    }

    static byte[] randomStr(Random rnd, int min, int max) {
        return randomStr(rnd, null, min, max);
    }

    static byte[] randomStr(Random rnd, byte[] prefix, int min, int max) {
        int size;
        if (min == max) {
            size = max;
        } else {
            size = min + rnd.nextInt(max - min);
        }

        int i;
        byte[] str;
        if (prefix == null) {
            i = 0;
            str = new byte[size];
        } else {
            i = prefix.length;
            str = new byte[i + size];
            System.arraycopy(prefix, 0, str, 0, i);
        }

        // Fill with printable ascii characters.
        for (; i < str.length; i++) {
            str[i] = (byte) (33 + rnd.nextInt(127 - 33));
        }

        return str;
    }
}

Related

  1. randomShort()
  2. randomSign()
  3. randomSimpleId()
  4. randomSleep()
  5. randomStateAbbr()
  6. randomSubList(List list, int sizeOfSubList)
  7. randomSuccess()
  8. randomUniformUniqueIntegerList(int sz)
  9. randomValue()