/*
* SimpleRandom.java xiaocao000 2009-7-30
*/
package com.xiaocao000.lang;
import java.util.Calendar;
import java.util.Date;
/**
* 简单随机数生成工具。
*
* @author xiaocao000
*/
public class SimpleRandom
{
private static final int BUFSIZE = 100;
private static double[] buf = new double[BUFSIZE];
static
{
for (int i = 0; i < buf.length; i++)
{
buf[i] = Math.random();
}
}
/**
* 获取一个随机byte值。
*
* @return 随机byte值。
*/
public static byte getByte()
{
return (byte) (nextRandom() * Byte.MAX_VALUE);
}
/**
* 获取一个介于[min, max)之间的随机byte值。
*
* @param min
* 随机byte值的下限(包含)
* @param max
* 随机byte值的上限(不包含)
* @return 介于[min, max)之间的随机byte值。
*/
public static byte getByte(byte min, byte max)
{
return (byte) (nextRandom() * (max - min) + min);
}
/**
* 获取一个随机short值。
*
* @return 随机short值。
*/
public static short getShort()
{
return (short) (nextRandom() * Short.MAX_VALUE);
}
/**
* 获取一个介于[min, max)之间的随机short值。
*
* @param min
* 随机short值的下限(包含)
* @param max
* 随机short值的上限(不包含)
* @return 介于[min, max)之间的随机short值。
*/
public static short getShort(short min, short max)
{
return (short) (nextRandom() * (max - min) + min);
}
/**
* 获取一个随机int值。
*
* @return 随机int值。
*/
public static int getInt()
{
return (int) (nextRandom() * Integer.MAX_VALUE);
}
/**
* 获取一个介于[min, max)之间的随机int值。
*
* @param min
* 随机int值的下限(包含)
* @param max
* 随机int值的上限(不包含)
* @return 介于[min, max)之间的随机int值。
*/
public static int getInt(int min, int max)
{
return (int) (nextRandom() * (max - min) + min);
}
/**
* 获取一个随机float值。
*
* @return 随机float值。
*/
public static float getFloat()
{
return (float) (nextRandom() * Float.MAX_VALUE);
}
/**
* 获取一个介于[min, max)之间的随机float值。
*
* @param min
* 随机float值的下限(包含)
* @param max
* 随机float值的上限(不包含)
* @return 介于[min, max)之间的随机float值。
*/
public static float getFloat(float min, float max)
{
return (float) (nextRandom() * (max - min) + min);
}
/**
* 获取一个随机long值。
*
* @return 随机long值。
*/
public static long getLong()
{
return (long) (nextRandom() * Long.MAX_VALUE);
}
/**
* 获取一个介于[min, max)之间的随机long值。
*
* @param min
* 随机long值的下限(包含)
* @param max
* 随机long值的上限(不包含)
* @return 介于[min, max)之间的随机long值。
*/
public static long getLong(long min, long max)
{
return (long) (nextRandom() * (max - min) + min);
}
/**
* 获取一个随机double值。
*
* @return 随机double值。
*/
public static double getDouble()
{
return (double) (nextRandom() * Double.MAX_VALUE);
}
/**
* 获取一个介于[min, max)之间的随机double值。
*
* @param min
* 随机double值的下限(包含)
* @param max
* 随机double值的上限(不包含)
* @return 介于[min, max)之间的随机double值。
*/
public static double getDouble(double min, double max)
{
return (double) (nextRandom() * (max - min) + min);
}
/**
* 获取一个随机boolean值。
*
* @return 随机boolean值
*/
public static boolean getBoolean()
{
return nextRandom() > 0.5;
}
/**
* 获取一个随机小写字母Character[a-z]值。
*
* @return 随机Character值
*/
public static char getLowerCharacter()
{
return (char) (getInt(0, 26) + 'a');
}
/**
* 获取一个随机大写字母Character[A-Z]值。
*
* @return 随机Character值
*/
public static char getUpperCharacter()
{
return (char) (getInt(0, 26) + 'A');
}
/**
* 获取一个随机字母Character[A-Z|a-z]值。
*
* @return 随机Character值
*/
public static char getCharacter()
{
// return getBoolean() ? getLowerCharacter() : getUpperCharacter();
return (char) (getInt(0, 26) + (getBoolean() ? 'A' : 'a'));
}
/**
* 返回一个长度为8的包含大小写字母的随机字符串。
*
* @return String
*/
public static String getString()
{
return getString(8);
}
/**
* 返回指定长度的包含大小写字母的随机字符串。
*
* @return String
*/
public static String getString(int len)
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < len; i++)
{
sb.append(getCharacter());
}
return sb.toString();
}
/**
* 获取随机的Calendar对像,年份介于[1970,2050]。
* @return 随机的Calendar对像
*/
public static Calendar getCalendar()
{
Calendar calendar = Calendar.getInstance();
calendar.set(getInt(1970, 2051), getInt(0, 12), getInt(1, 32),
getInt(0, 24), getInt(0, 60), getInt(0, 60));
return calendar;
}
/**
* 获取随机java.util.Date对象,年份介于[1970,2050]。
* @return 随机java.util.Date对象
*/
public static Date getDate()
{
return getCalendar().getTime();
}
/**
* 从随机数缓冲区获取一个double随机数。
*
* @return double随机数。
*/
private static double nextRandom()
{
int index = (int) (Math.random() * BUFSIZE);
// if (index == BUFSIZE)
// {
// index = BUFSIZE - 1;
// }
double result = buf[index];
buf[index] = Math.random();
return result;
}
}
SimpleRandom.java
最新推荐文章于 2022-01-05 02:27:19 发布