All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.pnc.buildagent.common.RandomUtils Maven / Gradle / Ivy

/**
 * JBoss, Home of Professional Open Source.
 * Copyright 2014 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * 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.
 */
package org.jboss.pnc.buildagent.common;

import java.util.Random;
import java.util.UUID;

/**
 * Created by Matej Lazar on 2015-01-06.
 */
public class RandomUtils {
    /**
     * Returns a pseudo-random number between min and max, inclusive.
     * The difference between min and max can be at most
     * Integer.MAX_VALUE - 1.
     *
     * @param min Minimum value
     * @param max Maximum value.  Must be greater than min.
     * @return Integer between min and max, inclusive.
     * @see Random#nextInt(int)
     * @see "http://stackoverflow.com/questions/363681/generating-random-integers-in-a-range-with-java"
     */
    public static int randInt(int min, int max) {

        // NOTE: Usually this should be a field rather than a method
        // variable so that it is not re-seeded every call.
        Random rand = new Random();

        // nextInt is normally exclusive of the top value,
        // so add 1 to make it inclusive
        int randomNum = rand.nextInt((max - min) + 1) + min;

        return randomNum;
    }

    public static String randString(int len) {
        if (len < 1) {
            return "";
        }
        String uuid = UUID.randomUUID().toString();
        if (len > uuid.length()) {
            int newLen = len - uuid.length();
            if (newLen < 0) {
                newLen = 0;
            }
            uuid += randString(newLen);
        }
        return uuid.substring(0, len);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy