zoukankan      html  css  js  c++  java
  • 获取随机金额:上限下限都要取到

    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;
    }
    

      

     

  • 相关阅读:
    [原创]启发式测试策略模型
    [原创]SSH框架介绍
    [原创]Sniffer工具培训
    [原创]浅谈Devops理念
    [原创接口测试技术介绍
    一个让我看了之后,痛哭不止的舞蹈!寻找有同感的人!
    多一点宽容,少一点抱怨;多一点付出,少一点指责。
    笼屉与夹肉馍(的制作方法) 之于 三层与MVC
    找工作、跳槽之旅——前言
    【自然框架.视频】基础设置(一)如何下载自然框架
  • 原文地址:https://www.cnblogs.com/time-on/p/9761516.html
Copyright © 2011-2022 走看看