Java tutorial
/* * 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.sunchenbin.store.feilong.core.util; import java.util.Random; import com.sunchenbin.store.feilong.core.tools.slf4j.Slf4jUtil; /** * * ?. * * <ul> * <li>Math.random() new Random(),?Random nextDouble()</li> * <li>Randomstatic?. JavaRandom??</li> * <li>?</li> * <li>???,?"?" n+1=(n*29+37) % 1000,%""?.</li> * </ul> * * @author feilong * @version 1.0.0 2010-4-5 ?10:55:19 * @version 1.0.7 2014519 ?6:45:01 * @since 1.0.0 * @see org.apache.commons.lang3.RandomStringUtils * @see org.apache.commons.lang3.RandomUtils */ public final class RandomUtil { /** * Random object used by random method. * * <p> * 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?? * </p> * * @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-?. * * <p> * ??{@link #JVM_RANDOM} * </p> * * @param number * ? * @return 0-? * @see java.lang.Math#random() */ public static long createRandom(Number number) { double random = JVM_RANDOM.nextDouble(); return (long) Math.floor(random * number.longValue()); } /** * ??. * * @param min * ? * @param max * * @return ?? */ public static long createRandom(Number min, Number max) { 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); } // ******************************************************************** /** * ???. * * <p> * ??{@link #JVM_RANDOM} * </p> * * @param length * ??.length?11 * @return ?? * @see #JVM_RANDOM */ public static long createRandomWithLength(int length) { // 0.0 ? 1.0 ? double 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,??. * * <p> * ?? ? * </p> * * @param str * ? * @param minLength * ? * @param maxLength * * @return ? */ public static String createRandomFromString(String str, int minLength, int maxLength) { 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,??. * * <p> * ?? * </p> * * @param str * ? * @param length * ? * @return ? */ public static String createRandomFromString(String str, int length) { 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) { int index = JVM_RANDOM.nextInt(j);// ?? ch[i] = str.charAt(index); } return new String(ch); } }