com.feilong.commons.core.util.RandomUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.feilong.commons.core.util.RandomUtil.java

Source

/*
 * Copyright (C) 2008 feilong (venusdrogon@163.com)
 *
 * 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 com.feilong.commons.core.util;

import java.util.Random;

import com.feilong.commons.core.log.Slf4jUtil;

/**
 * 
 * ?.
 * 
 * <p>
 * <ul>
 * <li>Math.random()  new Random(),?Random nextDouble()</li>
 * <li>Randomstatic?. JavaRandom??</li>
 * <li>?</li>
 * <li>????"?" n+1=(n*29+37) % 1000%""?.</li>
 * </ul>
 * </p>
 * 
 * @author  2010-4-5 ?10:55:19
 * @version 1.0.7 2014519 ?6:45:01
 * @since 1.0.0
 */
public final class RandomUtil {

    /**
     * Random object used by random method. <br>
     * This has to be not local to the random method so as to not return the same value in the same millisecond.<br>
     * Randomstatic?. JavaRandom??
     * 
     * @see org.apache.commons.lang.math.RandomUtils
     * @since 1.0.7
     */
    public static final Random JVM_RANDOM = new Random();

    /** Don't let anyone instantiate this class. */
    private RandomUtil() {
        //AssertionError?. ?????. ???.
        //see Effective Java 2nd
        throw new AssertionError("No " + getClass().getName() + " instances for you!");
    }

    /**
     * 0-?.<br>
     * <b>??{@link #JVM_RANDOM}</b>
     * 
     * @param number
     *            ?
     * @return 0-?
     */
    public static long createRandom(Number number) {
        // double random = Math.random();
        double random = JVM_RANDOM.nextDouble();
        return (long) Math.floor(random * number.longValue());
    }

    /**
     * ??.
     * 
     * @param min
     *            ?
     * @param max
     *            
     * @return ??
     * @throws IllegalArgumentException
     *             {@code if (maxLong < minLong)}
     */
    public static long createRandom(Number min, Number max) throws IllegalArgumentException {
        long maxLong = max.longValue();
        long minLong = min.longValue();
        if (maxLong < minLong) {
            String messagePattern = "maxLong:[{}] can not < minLong:[{}]";
            throw new IllegalArgumentException(Slf4jUtil.formatMessage(messagePattern, maxLong, minLong));
        }
        long cha = maxLong - minLong;
        return minLong + createRandom(cha);
    }

    // ********************************************************************

    /**
     * ???<br>
     * <b>??{@link #JVM_RANDOM}</b>
     * 
     * @param length
     *            ??.length?11
     * @return ??
     * @see #JVM_RANDOM
     */
    public static long createRandomWithLength(int length) {
        //  0.0 ? 1.0 ? double 
        // double random = Math.random();
        double random = JVM_RANDOM.nextDouble();
        if (random < 0.1) {// ? 0.09346924349151808
            random = random + 0.1;
        }

        // ****************************************
        long num = 1;
        for (int i = 0; i < length; ++i) {
            num = num * 10;
        }
        return (long) (random * num);
    }

    // ****************************************************************

    /**
     * ??char,??.<br>
     * ?? ?
     * 
     * @param minLength
     *            ?
     * @param maxLength
     *            
     * @param str
     *            ?
     * @return ?
     * @throws NullPointerException
     *             if isNullOrEmpty(str)
     * @throws IllegalArgumentException
     *             {@code if maxLength<=0 or if (maxLength < minLength)}
     */
    public static String createRandomFromString(String str, int minLength, int maxLength)
            throws IllegalArgumentException, NullPointerException {
        if (Validator.isNullOrEmpty(str)) {
            throw new NullPointerException("the str is null or empty!");
        }

        if (maxLength <= 0) {
            String messagePattern = "maxLength:[{}] can not zero";
            throw new IllegalArgumentException(Slf4jUtil.formatMessage(messagePattern, maxLength));
        }

        if (maxLength < minLength) {
            String messagePattern = "maxLength:[{}] can not < minLength:[{}]";
            throw new IllegalArgumentException(Slf4jUtil.formatMessage(messagePattern, maxLength, minLength));
        }
        long length = createRandom(minLength, maxLength);
        return createRandomFromString(str, (int) length);
    }

    /**
     * ??char,??.<br>
     * ?? 
     * 
     * @param str
     *            ?
     * @param length
     *            ?
     * @return ?
     * @throws NullPointerException
     *             if isNullOrEmpty(str)
     * @throws IllegalArgumentException
     *             {@code if length<=0}
     */
    public static String createRandomFromString(String str, int length)
            throws IllegalArgumentException, NullPointerException {
        if (Validator.isNullOrEmpty(str)) {
            throw new NullPointerException("the str is null or empty!");
        }
        if (length <= 0) {
            String messagePattern = "length:[{}] can not <=0";
            throw new IllegalArgumentException(Slf4jUtil.formatMessage(messagePattern, length));
        }

        char[] ch = new char[length];
        int j = str.length();
        for (int i = 0; i < length; ++i) {
            // Random random = new Random();
            int index = JVM_RANDOM.nextInt(j);// ??
            ch[i] = str.charAt(index);
        }
        return new String(ch);
    }
}