The helper class provides three methods:
(1) addweightnumber (INT weight, int num): assign a weight to a num. This weight indicates the probability that this number is obtained at random. The weight is large, then, the probability obtained is large, and the weight is small, the probability obtained is small.
(2) addweightnumrange (INT weight, int numfrom, int numto, int... numexcludes); at the same time, assign weights to multiple consecutive numbers. Finally, this parameter can exclude exceptional numbers, such as addweightnumrange (,); 1 ~ 10 (except 5) digits are weighted to 5;
(3) getnextint (); after you assign a weight, this method will then take out one of the numbers you grant the weight;
Advantage: The advantage of this method and new random (). nextint () is that you can assign weights to each number and randomly retrieve the specified number set;
Package Org. xiazdong. util; import Java. util. arraylist; import Java. util. list; import Java. util. random; public class integerweightrandom {private list <integer> weights = new arraylist <integer> (); private list <integer> numbers = new arraylist <list <integer> (); Private integer totalweight; private random = new random (); Public integerweightrandom () {} private integer gettotalweight () {If (totalweigh T = NULL) {totalweight = 0; For (integer weight: weights) {totalweight + = weight;} return totalweight;}/** assign a weight to a number, one weight may correspond to multiple numbers, therefore, the list <integer> weight element corresponds to a list <integer> List <integer> ** in the list <integer> numbers. // This function indicates that a weight corresponds to a numberpublic void addweightnumber (int weight, int num) {weights. add (weight); List <integer> List = new arraylist <integer> (); list. add (Num); numbers. add (list);} // This function table Indicates that one weight corresponds to multiple numberpublic void addweightnumrange (INT weight, int numfrom, int numto, int... numexcludes) {weights. add (weight); List <integer> List = new arraylist <integer> (); For (INT I = numfrom; I <= numto; I ++) {Boolean exclude = false; For (INT numexclude: numexcludes) {if (I = numexclude) {exclude = true ;}} if (! Exclude) {list. add (I) ;}} numbers. add (list);}/** example: * If weight of Number 8 is 1 and weight of Number 9 is 2, the range of totalweight = 3,8 is (0, 1], the range of 9 is [2, 3]. If the random number is 2, then ** // This function is a random integer in the range of 9 (based on the weight probability) public integer getnextint () {int weightrandom = random. nextint (gettotalweight (); int weightcount = 0; For (INT I = 0; I <weights. size (); I ++) {int Weight = weights. get (I); If (weightrandom> = weightcount & weightrandom <weightcount + weight) {list <integer> numlist = numbers. get (I); If (numlist. size () = 1) {return numlist. get (0);} int numrandom = random. nextint (numlist. size (); Return numlist. get (numrandom);} weightcount + = weight;} return NULL ;}}
Test class:
package test.org.xiazdong.util;import org.junit.Test;import org.xiazdong.util.IntegerWeightRandom;public class IntegerWeightRandomTest {@Testpublic void testGetNextInt(){IntegerWeightRandom random = new IntegerWeightRandom();random.addWeightNumber(1, 0);random.addWeightNumber(5, 1);random.addWeightNumber(9, 2);random.addWeightNumber(15, 3);random.addWeightNumber(20, 4);random.addWeightNumber(30, 5);random.addWeightNumber(40, 7);random.addWeightNumber(50, 6);random.addWeightNumber(60, 8);random.addWeightNumber(70, 9);for(int i=0;i<10;i++){System.out.println(random.getNextInt());}}}