⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 randomnum.java

📁 java阿里巴巴代码
💻 JAVA
字号:
package tools.util;

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

/**
 * Created by zywang
 * Date: 2006-11-13
 * Time: 10:28:30
 */
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 {

        if (secure) {
            try {
                if (provider != null)
                    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;
     //   Integer rand;
        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.");

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -