mock probability with lottery
2015-09-23 12:22
363 查看
public class TestLottery { static int BASE_RANDOM_NUM=1000; private static int getProbabilityIndex(final List<Double> probabilityList){ Map<Integer,Double> mapRates= calculateBaseProbability(probabilityList); int randomProbability=RandomUtils.nextInt(BASE_RANDOM_NUM); Set<Entry<Integer, Double>> sets=mapRates.entrySet(); for(Entry<Integer, Double> entry:sets){ if (entry.getValue() >= randomProbability && entry.getValue() <= BASE_RANDOM_NUM) { return entry.getKey(); } } return -1; } private static Map<Integer,Double> calculateBaseProbability(final List<Double> probabilityList){ Map<Integer,Double> mapRates=new HashMap<Integer, Double>(); double sumProbability=0.0; int index=0; Iterator<Double> itor= probabilityList.iterator(); while(itor.hasNext()){ double probability=itor.next(); if(probability>0){ sumProbability+=probability; mapRates.put(index, sumProbability*BASE_RANDOM_NUM); } index++; } return mapRates; } public static List<Double> createDoubleList(){ List<Double> orignalRates=new ArrayList<Double>(); orignalRates.add(0.0); orignalRates.add(0.0); orignalRates.add(0.6); orignalRates.add(0.3); orignalRates.add(0.1); return orignalRates; } public static void main(String[] args) { Map<Integer,Double> proMap=new HashMap<Integer, Double>(); double sumCount=100000; List<Double> orignalRates=createDoubleList(); for (double i = 0; i < sumCount; i++) { int index =getProbabilityIndex(orignalRates); if(proMap.containsKey(index)){ proMap.put(index, proMap.get(index)+1.0); }else{ proMap.put(index, 1.0); } } Set<Entry<Integer, Double>> setEntry=proMap.entrySet(); for(Entry<Integer, Double> entry:setEntry){ System.out.println( entry.getKey()+"("+entry.getValue()+")->rate:"+ entry.getValue()/sumCount); } } }
相关文章推荐
- Mockito - Wanted but not invoked: Actually, there were zero interactions with this mock
- 【bzoj2318】Spoj4060 game with probability Problem
- Mock Objects to the Rescue! Test Your .NET Code with NMock
- unit test use EasyMock:Possible matches are marked with (+1):
- [BZOJ2318][SPOJ4060]Game with probability Problem 概率DP
- BZOJ2318: Spoj4060 game with probability Problem
- BZOJ 2318: Spoj4060 game with probability Problem 概率
- bzoj2318 Spoj4060 game with probability Problem
- Machine Learning--Classifying with probability theory: Naive Bayes
- org.mockito.exceptions.misusing.CannotStubVoidMethodWithReturnValue
- [BZOJ2318]Spoj4060 game with probability Problem(概率dp)
- 概率DP Spoj4060 game with probability Problem
- 【BZOJ2318】【SPOJ4060】Game with Probability Problem
- Moq - Mock with LINQ
- bzoj 2318: Spoj4060 game with probability Problem (概率与期望DP)
- Randow Id generator with array of probability
- Unit tests with Mockito - Tutorial
- Working with Symbols (在Balsamiq Mockups中复用自定义控件和页面模板)
- 【BZOJ 2318】 2318: Spoj4060 game with probability Problem(概率DP)
- BZOJ 2318: Spoj4060 game with probability Problem( 概率dp )