写了一个获得随机数的类,大家看看!
import java.util.*;
import java.security.SecureRandom;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
/**
* 一个获得随机数的类
* <p>Copyright: 2004</p>
* @author Filippo Guan
* @version 1.1
*/
public class RandomStr
{
static String randomstr;
static boolean allchars = false;
//欠缺是8位字符串
static Integer length = new Integer(8);
static HashMap hmap;
static ArrayList lower = null;
static ArrayList upper = null;
static char[] single = null;
static int singlecount = 0;
static boolean singles = false;
static String algorithm = null;
static String provider = null;
static boolean secure = false;
static Random random = null;
static SecureRandom secrandom = null;
private static float getFloat()
{
if (random == null)
{
return secrandom.nextFloat();
}
else
{
return random.nextFloat();
}
}
public static void generateRandomObject() throws Exception
{
// check to see if the object is a SecureRandom object
if (secure)
{
try
{
// get an instance of a SecureRandom object
if (provider != null)
{
random = SecureRandom.getInstance(algorithm, provider);
}
else
{
random = SecureRandom.getInstance(algorithm);
}
}
catch (NoSuchAlgorithmException ne)
{
throw new Exception(ne.getMessage());
}
catch (NoSuchProviderException pe)
{
throw new Exception(pe.getMessage());
}
}
else
{
random = new Random();
}
}
/**
* generate the random string
*/
private static void generaterandom()
{
if (allchars)
{
for (int i = 0; i < length.intValue(); i++)
{
randomstr = randomstr + new Character((char)((int) 34 +((int)(getFloat() * 93)))).toString();
}
}
else if (singles)
{
// check if there are single chars to be included
if (upper.size() == 3)
{
// check for the number of ranges max 3 uppercase lowercase digits build the random string
for (int i = 0; i < length.intValue(); i++)
{
// you have four groups to choose a random number from, to make
// the choice a little more random select a number out of 100 get a random number even or odd
if (((int) (getFloat() * 100)) % 2 == 0)
{
// the number was even get another number even or odd
if (((int) (getFloat() * 100)) % 2 == 0)
{
// choose a random char from the single char group
randomstr = randomstr + randomSingle().toString();
}
else
{
// get a random char from the first range
randomstr = randomstr + randomChar((Character)lower.get(2),(Character)upper.get(2)).toString();
}
}
else
{
// the number was odd
if (((int) (getFloat() * 100)) % 2 == 0)
{
// choose a random char from the second range
randomstr = randomstr + randomChar((Character)lower.get(1),(Character)upper.get(1)).toString();
}
else
{
// choose a random char from the third range
randomstr = randomstr + randomChar((Character)lower.get(0),(Character)upper.get(0)).toString();
}
}
}
}
else if (upper.size() == 2)
{
// single chars are to be included choose a random char from
// two different ranges build the random char from single chars and two ranges
for (int i = 0; i < length.intValue(); i++)
{
// select the single chars or a range to get each random charfrom
if (((int)(getFloat() * 100)) % 2 == 0)
{
// get random char from the single chars
randomstr = randomstr + randomSingle().toString();
}
else if (((int) (getFloat() * 100)) % 2 == 0)
{
// get the random char from the first range
randomstr = randomstr + randomChar((Character)lower.get(1),(Character)upper.get(1)).toString();
}
else
{
// get the random char from the second range
randomstr = randomstr + randomChar((Character)lower.get(0),(Character)upper.get(0)).toString();
}
}
}
else if (upper.size() == 1)
{
// build the random string from single chars and one range
for (int i = 0; i < length.intValue(); i++)
{
if (((int) getFloat() * 100) % 2 == 0)
{
// get a random single char
randomstr = randomstr + randomSingle().toString();
}
else
{
// get a random char from the range
randomstr = randomstr + randomChar((Character)lower.get(0),(Character)upper.get(0)).toString();
}
}
}
else
{
// build the rand string from single chars
for (int i = 0; i < length.intValue(); i++)
{
randomstr = randomstr + randomSingle().toString();
}
}
}
else
{
// no single chars are to be included in the random string
if (upper.size() == 3)
{
// build random strng from three ranges
for (int i = 0; i < length.intValue(); i++)
{
if (((int) (getFloat() * 100)) % 2 == 0)
{
// get random char from first range
randomstr = randomstr + randomChar((Character)lower.get(2),(Character)upper.get(2)).toString();
}
else if (((int) (getFloat() * 100)) % 2 == 0)
{
// get random char form second range
randomstr = randomstr + randomChar((Character)lower.get(1),(Character)upper.get(1)).toString();
}
else
{
// get random char from third range
randomstr = randomstr + randomChar((Character)lower.get(0),(Character)upper.get(0)).toString();
}
}
}
else if (upper.size() == 2)
{
// build random string from two ranges
for (int i = 0; i < length.intValue(); i++)
{
if (((int) (getFloat() * 100)) % 2 == 0)
{
// get random char from first range
randomstr = randomstr + randomChar((Character)lower.get(1),(Character)upper.get(1)).toString();
}
else
{
// get random char from second range
randomstr = randomstr + randomChar((Character)lower.get(0),(Character)upper.get(0)).toString();
}
}
}
else
{
// build random string
for (int i = 0; i < length.intValue(); i++)
{
// get random char from only range
randomstr = randomstr + randomChar((Character)lower.get(0),(Character)upper.get(0)).toString();
}
}
}
}