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

Java tutorial

Introduction

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

Source

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

import java.util.Random;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.Validate;

import com.feilong.tools.slf4j.Slf4jUtil;

/**
 * ?.
 * 
 * <ul>
 * <li>{@link java.lang.Math#random()} new Random(),?Random nextDouble()</li>
 * <li> {@link java.util.Random}(static)?.Java {@link java.util.Random} (??);</li>
 * <li>?</li>
 * <li>???,? "?":n+1=(n*29+37) % 1000,%""?.</li>
 * </ul>
 *
 * @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
 * @see java.lang.Math#random()
 * @see org.apache.commons.lang3.RandomUtils
 * @see org.apache.commons.lang3.RandomStringUtils
 * @since 1.0.0
 */
public final class RandomUtil {

    /**
     * Random object used by random method.
     * 
     * <p>
     * ????,???.<br>
     * Random(static)?. JavaRandom(??);
     * </p>
     * 
     * @see org.apache.commons.lang.math.RandomUtils
     * @since 1.0.7
     */
    private 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-<b>(maxValue)</b>?.
     * 
     * <h3>:</h3>
     * <blockquote>
     * 
     * <pre class="code">
     * RandomUtil.createRandom(8)
     * ?8?
     * 
     * ?? 3
     * </pre>
     * 
     * </blockquote>
     * 
     * @param maxValue
     *            ?
     * @return  <code>maxValue</code> null, {@link NullPointerException}
     * @throws NullPointerException
     *              <code>maxValue</code> null
     */
    public static long createRandom(Number maxValue) {
        Validate.notNull(maxValue, "maxValue can't be null!");
        //  0.0 ? 1.0 ? double 
        double random = JVM_RANDOM.nextDouble();
        return (long) Math.floor(random * maxValue.longValue());//double ???
    }

    /**
     * ?(?)<code>minInclusiveValue</code>(??)<code>maxExclusiveValue</code>?.
     * 
     * <h3>:</h3>
     * <blockquote>
     * 
     * <pre class="code">
     * RandomUtil.createRandom(10, 20)
     * 10-20?
     * 
     * ?? 12
     * </pre>
     * 
     * </blockquote>
     * 
     * @param minInclusiveValue
     *            ?
     * @param maxExclusiveValue
     *            
     * @return  <code>minInclusiveValue</code> null, {@link NullPointerException};<br>
     *          <code>maxExclusiveValue</code> null, {@link NullPointerException};<br>
     *          <code>minInclusiveValue</code>{@code <}<code>maxExclusiveValue</code>,{@link IllegalArgumentException}<br>
     *          <code>minInclusiveValue</code>{@code =}<code>maxExclusiveValue</code>, <code>minLong</code>
     * 
     * @see org.apache.commons.lang3.RandomUtils#nextInt(int, int)
     * @see org.apache.commons.lang3.RandomUtils#nextLong(long, long)
     * @see org.apache.commons.lang3.RandomUtils#nextFloat(float, float)
     * @see org.apache.commons.lang3.RandomUtils#nextDouble(double, double)
     */
    public static long createRandom(Number minInclusiveValue, Number maxExclusiveValue) {
        Validate.notNull(minInclusiveValue, "minInclusiveValue can't be null!");
        Validate.notNull(maxExclusiveValue, "maxExclusiveValue can't be null!");

        long minLong = minInclusiveValue.longValue();
        long maxLong = maxExclusiveValue.longValue();

        Validate.isTrue(maxLong >= minLong,
                Slf4jUtil.format("minInclusiveValue:[{}] can not < maxExclusiveValue:[{}]", maxLong, minLong));
        return RandomUtils.nextLong(minLong, maxLong);
    }

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

    /**
     * ?<code>length</code> <b>?</b>.
     * 
     * <h3>:</h3>
     * <blockquote>
     * 
     * <pre class="code">
     * RandomUtil.createRandomWithLength(2)
     * ?? 89
     * </pre>
     * 
     * </blockquote>
     *
     * @param length
     *            ??.
     * @return  <code>length</code> {@code <=0} , {@link IllegalArgumentException}
     */
    public static long createRandomWithLength(int length) {
        Validate.isTrue(length > 0, "length:[%s] must >0", length);
        long num = 1;
        for (int i = 0; i < length; ++i) {
            num = num * 10;
        }

        //  0.0 ? 1.0 ? double 
        double random = JVM_RANDOM.nextDouble();
        random = random < 0.1 ? random + 0.1 : random;// ? 0.09346924349151808
        return (long) (random * num);
    }

    // ******************************createRandomFromString**********************************
    /**
     * ??<code>char</code>,?<code>length</code>.
     * 
     * <h3>:</h3>
     * <blockquote>
     * 
     * <pre class="code">
     * RandomUtil.createRandomFromString({@link com.feilong.core.Alphabet#DECIMAL_AND_LETTERS Alphabet.DECIMAL_AND_LETTERS}, 5)
     * 
     *  {@link com.feilong.core.Alphabet#DECIMAL_AND_LETTERS  Alphabet.DECIMAL_AND_LETTERS} ??,?5
     * ??IFSMB
     * </pre>
     * 
     * </blockquote>
     * 
     * @param str
     *            ?,{@link com.feilong.core.Alphabet#DECIMAL_AND_LETTERS}
     * @param length
     *            , 5
     * @return  <code>str</code> null, {@link NullPointerException}<br>
     *          <code>str</code> blank, {@link IllegalArgumentException}<br>
     *          <code>length</code> {@code <=0},  {@link IllegalArgumentException}
     * 
     * @see org.apache.commons.lang3.RandomStringUtils#random(int, String)
     */
    public static String createRandomFromString(String str, int length) {
        Validate.notBlank(str, "str can't be null/empty!");
        Validate.isTrue(length > 0, Slf4jUtil.format("length:[{}] can not <=0", length));
        return RandomStringUtils.random(length, str);
    }

    /**
     * ??char,?(?)<code>minLength</code>,(??)<code>maxLength</code>?.
     * 
     * <h3>:</h3>
     * <blockquote>
     * 
     * <pre class="code">
     * RandomUtil.createRandomFromString({@link com.feilong.core.Alphabet#DECIMAL Alphabet.DECIMAL}, 8, 20)
     * {@link com.feilong.core.Alphabet#DECIMAL Alphabet.DECIMAL}??,?<b>?8</b>,<b>20</b>
     * 
     * ?? 142853574998970631
     * </pre>
     * 
     * </blockquote>
     *
     * @param str
     *            ?, {@link com.feilong.core.Alphabet#DECIMAL}
     * @param minLength
     *            ? , 8
     * @param maxLength
     *            , 20
     * 
     * @return  <code>str</code> null, {@link NullPointerException}<br>
     *          <code>str</code> blank, {@link IllegalArgumentException}<br>
     *          <code>maxLength</code> {@code <=}0 , {@link IllegalArgumentException}<br>
     *          <code>maxLength</code> {@code <} minLength, {@link IllegalArgumentException}
     * @see #createRandomFromString(String, int)
     */
    public static String createRandomFromString(String str, int minLength, int maxLength) {
        Validate.notBlank(str, "str can't be null/empty!");

        Validate.isTrue(maxLength > 0, Slf4jUtil.format("maxLength:[{}] can not zero", maxLength));
        Validate.isTrue(maxLength >= minLength,
                Slf4jUtil.format("maxLength:[{}] can not < minLength:[{}]", maxLength, minLength));

        long length = createRandom(minLength, maxLength);
        return createRandomFromString(str, (int) length);
    }

}