根据权重获取随机值

     有一组优惠 A-20优惠, B-30元优惠, C-5元优惠  当用户满足条件时随机返回一个优惠;在活动预算一定的情况下需要保证C类优惠券中奖概率最高,B类优惠券最低;以下是权重的实现;    

    

   

/**
 * Copyright 2014-2015, , Inc. All Rights Reserved.
 * 
 * Date: 2016年1月23日
 */

package org.demo.core;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/***
 * 
 * Desc:TODO
 * 
 * @author zhangwei<wei.zw@corp.netease.com>
 * @since 2016年1月23日 下午1:35:16
 * @version v 0.1
 */
public class WeightRandomTest {
	private static Map<Double, String> weightMapping = new HashMap<>();

	static {
		weightMapping.put(30d, "A");
		weightMapping.put(20D, "B");
		weightMapping.put(50D, "C");
	}

	public static void main(String[] args) {
		int c = 0;
		int a = 0;
		int b = 0;
		for (int i = 0; i < 2000000; i++) {
			String str = WeightRandomUtil.getWeightRandom(weightMapping);
			if ("A".equals(str)) {
				a++;
			} else if ("B".equals(str)) {
				b++;
			} else if ("C".equals(str)) {
				c++;
			}
		}

		System.out.println(c / 2000000d);
		System.out.println(b / 2000000d);
		System.out.println(a / 2000000d);
	}
}

class WeightRandomUtil {

	/***
	 * 计算权重总和
	 * 
	 * @param weightArrays
	 * @return
	 * @author zhangwei<wei.zw@corp.netease.com>
	 */
	private static double weightSum(Set<Double> weights) {
		double weightSum = 0;
		for (double weightValue : weights) {
			weightSum += weightValue;
		}
		return weightSum;
	}

	/***
	 * 
	 * 
	 * @param weightArrays
	 *            权重数组
	 * @return 返回数据
	 * @author zhangwei<wei.zw@corp.netease.com>
	 * @param <T>
	 */
	public static <T> T getWeightRandom(Map<Double, T> weightValueMapping) {
		double weightSum = weightSum(weightValueMapping.keySet());
		double stepWeightSum = 0;
		List<Double> list = new ArrayList<>(weightValueMapping.keySet());
		Collections.sort(list, new Comparator<Double>() {

			@Override
			public int compare(Double o1, Double o2) {
				return (int) (o2 - o1);
			}

		});
		double r = Math.random();
		for (double weight : list) {
			// 计算权重值
			stepWeightSum += weight;
			// 如果随机数落在了权重区间则返回索引值
			if (r <= stepWeightSum / weightSum) {
				return weightValueMapping.get(weight);
			}
		}
		return null;
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值