Java 随机数字 浮点数 字符串产生

JSP中经常用到随机数字或字符(如密码产生 sessionid产生)可以使用taglib标签产生,本人使用bean产生随机数:

1.可以产生10000000 和 99999999之间随机数

2.可以产生规定数字之间的随机数,如25 100之间

3.可以使用algorithm 和 provider产生一个SecureRandom随机数字或字符串

object instead of a Random object: 71

4.可以产生浮点浮点随机数;

5.可以产生a-zA-Z0-9之间的规定长度个字符串

6.可以产生规定长度的小写字母字符串

7.可以产生任意字符串.

以下jsp bean 在Tomcat Win2000 下调试通过。[code]/**

  • 随机数字bean
    */
    package com.ossez;

import java.util.*;
import java.security.SecureRandom;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

public class RandomNum {

private Long randomnum = null;
private Float randomfloat = null;
private boolean floatvalue = false;
private long upper = 100;
private long lower = 0;
private String algorithm = null;
private String provider = null;
private boolean secure = false;
private Random random = null;
private SecureRandom secrandom = null;

private final float getFloat() {
	if (random == null)
		return secrandom.nextFloat();
	else
		return random.nextFloat();
}

public final 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)
				// search for algorithm in package provider
				secrandom = SecureRandom.getInstance(algorithm, provider);
			else
				secrandom = 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 number
 * 
 */
private final void generaterandom() {

	int tmprandom = 0; // temp storage for random generated number
	Integer rand;

	// check to see if float value is expected
	if (floatvalue)
		randomfloat = new Float(getFloat());
	else
		randomnum = new Long(lower
				+ (long) ((getFloat() * (upper - lower))));
}

public final Number getRandom() {
	generaterandom();
	if (floatvalue)
		return randomfloat;
	else
		return randomnum;
}

public final void setRange(long low, long up) {

	// set the upper and lower bound of the range
	lower = low;
	upper = up;

	// check to see if a float value is expected
	if ((lower == 0) && (upper == 1))
		floatvalue = true;
}

/**
 * set the algorithm name
 * 
 * @param value
 *            name of the algorithm to use for a SecureRandom object
 * 
 */
public final void setAlgorithm(String value) {
	algorithm = value;
	secure = true; // a SecureRandom object is to be used
}

public final void setProvider(String value) {
	provider = value;
}

public final void setRange(String value) throws Exception {
	try {
		upper = new Integer(value.substring(value.indexOf('-') + 1))
				.longValue();
	} catch (Exception ex) {
		throw new Exception("upper attribute could not be"
				+ " turned into an Integer default value was used");
	}

	try {
		lower = new Integer(value.substring(0, value.indexOf('-')))
				.longValue();
	} catch (Exception ex) {
		throw new Exception("lower attribute could not be"
				+ " turned into an Integer default value was used");
	}

	if ((lower == 0) && (upper == 1))
		floatvalue = true;

	if (upper < lower)
		throw new Exception("You can't have a range where the lowerbound"
				+ " is higher than the upperbound.");

}

}
[/code]