• 获取随机金额:上限下限都要取到


    1、随机金额为最大值与最小值之间的二位小数

    /**
    	 * 获取随机券金额
    	 * 随机金额为最大值与最小值之间的二位小数。
    	 * @param maxValue,minValue
    	 * @return newrandomNum
    	 */
    	private BigDecimal getValue(BigDecimal maxValue ,BigDecimal minValue){
    		double randomNum =  Math.round((Math.random()*(maxValue.doubleValue() - minValue.doubleValue()) + minValue.doubleValue()) * 100);
    		DecimalFormat dfm = new DecimalFormat ("00");
    		String newrandomNum = dfm.format(randomNum);
    		return BigDecimal.valueOf(Double.parseDouble(newrandomNum)).divide(new BigDecimal(100));
    	}
    
    	public static void main(String[] args) {
    	    Map<BigDecimal, Integer> map = new HashMap<BigDecimal, Integer>();
    
    		BigDecimal maxValue = new BigDecimal(8);
    		BigDecimal minValue = new BigDecimal(3);
    
    		for(int i=0; i<10000; i++){
                BigDecimal b = new BaseSendCouponServiceImpl().getValue(maxValue,minValue);
    
                if(map.get(b) == null){
                    map.put(b, 1);
                } else {
                    map.put(b, map.get(b) + 1);
                }
    
    		}
    
    		for(Map.Entry entry:map.entrySet()) {
                System.out.println(entry.getKey() + "   " + entry.getValue());
            }
    	}
    

      

    2、随机金额为最大值与最小值之间的整数

    private BigDecimal getValue(BigDecimal maxValue ,BigDecimal minValue){
    		double randomNum =  Math.round((Math.random()*(maxValue.doubleValue() - minValue.doubleValue()) + minValue.doubleValue()));
    		DecimalFormat dfm = new DecimalFormat ("00");
    		String newrandomNum = dfm.format(randomNum);
    		return BigDecimal.valueOf(Double.parseDouble(newrandomNum));
    	}
    
    	public static void main(String[] args) {
    	    Map<BigDecimal, Integer> map = new HashMap<BigDecimal, Integer>();
    
    		BigDecimal maxValue = new BigDecimal(8);
    		BigDecimal minValue = new BigDecimal(3);
    
    		for(int i=0; i<100000; i++){
                BigDecimal b = new BaseSendCouponServiceImpl().getValue(maxValue,minValue);
    
                if(map.get(b) == null){
                    map.put(b, 1);
                } else {
                    map.put(b, map.get(b) + 1);
                }
    
    		}
    
    		for(Map.Entry entry:map.entrySet()) {
                System.out.println(entry.getKey() + "   " + entry.getValue());
            }
    	}
    

     

    3、同事写的

    public static BigDecimal getRandomValueBetween(BigDecimal maxValue, BigDecimal minValue) {
    
    if (maxValue == null || minValue == null) {
    return BigDecimal.ZERO;
    }
    
    BigDecimal factor = new BigDecimal(new Random().nextInt(101) ).divide(new BigDecimal(100.00));
    BigDecimal randomNum = factor.multiply(maxValue.subtract(minValue).add(minValue)).setScale(1, BigDecimal.ROUND_HALF_UP);
    return randomNum;
    }
    

      

     

  • 相关阅读:
    struts2 文件上传 中的空指针问题
    大事难事,看担当;人生最大的自由不是想干啥就干啥,而是想不干啥就不干啥!
    一条灰色的线<HR align=center width=700 color=#cccccc SIZE=1>
    jsp九大内置对象及其作用域
    2012年Java认证考试报考指南汇总
    Elance是全球最大的外包网站。
    •《深入理解Java虚拟机:JVM高级特性与最佳实践》
    新浪微博信息
    输入框中的添加回车事件
    font标签的一个小提示 不要加分号
  • 原文地址:https://www.cnblogs.com/time-on/p/9761516.html
Copyright © 2020-2023  润新知