随机获得用户定义的整数实现

辅助类提供了3个方法:

(1)addWeightNumber(int weight,int num):为某个num赋予weight权重,此权重代表此数字在随机获取时的获得概率;权重大, 则获得的概率就大,权重小,则获得的概率就小。

(2)addWeightNumRange(int weight,int numfrom,int numto,int ... numExcludes);同时为连续多个数字赋予权重,最后这个参数可以排除例外数字,比如addWeightNumRange(5,1,10,5); 表示为1~10(除去5)的数字赋予权重5;

(3)getNextInt();在你赋予权重之后,此方法会随即在你赋予权重的这些数字之中取出一个;

优点:此方法和new Random().nextInt()的优势在于,可以为每个数字赋予权重,并且可以随机取出用户指定的数字集合;

 

  1. package org.xiazdong.util;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.Random;  
  6.   
  7. public class IntegerWeightRandom {  
  8.     private List<Integer> weights = new ArrayList<Integer>();  
  9.     private List<List<Integer>> numbers = new ArrayList<List<Integer>>();  
  10.     private Integer totalWeight;  
  11.     private Random random = new Random();  
  12.     public IntegerWeightRandom() {  
  13.           
  14.     }  
  15.       
  16.     private Integer getTotalWeight(){  
  17.         if(totalWeight == null){  
  18.             totalWeight = 0;  
  19.             for(Integer weight:weights){  
  20.                 totalWeight += weight;  
  21.             }  
  22.         }  
  23.         return totalWeight;  
  24.     }  
  25.     /* 
  26.      * 为某个number赋予一个weight,可能一个weight对应多个numbers,因此List<Integer>weight中一个元素对应List<List<Integer>>numbers中的一个List<Integer> 
  27.      * */  
  28.     //此函数表示一个weight对应一个number  
  29.     public void addWeightNumber(int weight,int num){      
  30.         weights.add(weight);  
  31.         List<Integer> list = new ArrayList<Integer>();  
  32.         list.add(num);  
  33.         numbers.add(list);  
  34.     }  
  35.     //此函数表示一个weight对应多个number  
  36.     public void addWeightNumRange(int weight,int numFrom,int numTo,int...numExcludes){  
  37.         weights.add(weight);  
  38.         List<Integer> list = new ArrayList<Integer>();  
  39.         for(int i = numFrom;i <= numTo;i++){  
  40.             boolean exclude = false;  
  41.             for(int numExclude:numExcludes){  
  42.                 if(i == numExclude){  
  43.                     exclude = true;  
  44.                 }  
  45.             }  
  46.             if(!exclude){  
  47.                 list.add(i);  
  48.             }  
  49.         }  
  50.         numbers.add(list);  
  51.     }  
  52.     /* 
  53.      * 举例: 
  54.      * 如果数字8的weight为1,数字9的weight为2,则totalWeight=3,8的范围为(0,1],9的范围为[2,3]如果随机数为2,则在9的范围内 
  55.      * */  
  56.     //此函数为随机取一个整数(按照权重概率取)  
  57.     public Integer getNextInt(){  
  58.         int weightRandom = random.nextInt(getTotalWeight());  
  59.         int weightCount = 0;  
  60.         for(int i = 0;i < weights.size();i++){  
  61.             int weight = weights.get(i);  
  62.             if(weightRandom >= weightCount && weightRandom < weightCount + weight){  
  63.                 List<Integer> numList = numbers.get(i);  
  64.                 if(numList.size() == 1){  
  65.                     return numList.get(0);  
  66.                 }  
  67.                 int numRandom = random.nextInt(numList.size());  
  68.                 return numList.get(numRandom);  
  69.             }  
  70.             weightCount += weight;  
  71.         }  
  72.           
  73.         return null;  
  74.     }  
  75.   
  76. }  

测试类:

  1. package test.org.xiazdong.util;  
  2.   
  3. import org.junit.Test;  
  4.   
  5. import org.xiazdong.util.IntegerWeightRandom;  
  6.   
  7. public class IntegerWeightRandomTest {  
  8.     @Test  
  9.     public void testGetNextInt(){  
  10.         IntegerWeightRandom random = new IntegerWeightRandom();  
  11.         random.addWeightNumber(10);  
  12.         random.addWeightNumber(51);  
  13.         random.addWeightNumber(92);  
  14.         random.addWeightNumber(153);  
  15.         random.addWeightNumber(204);  
  16.         random.addWeightNumber(305);  
  17.         random.addWeightNumber(407);  
  18.         random.addWeightNumber(506);  
  19.         random.addWeightNumber(608);  
  20.         random.addWeightNumber(709);  
  21.         for(int i=0;i<10;i++){  
  22.             System.out.println(random.getNextInt());  
  23.         }  
  24.           
  25.     }  
  26. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值