JAVA实现概率计算(数字不同范围按照不同几率产生随机数)
转载于JAVA实现概率计算(数字不同范围按照不同几率产生随机数)
原文是int 转成了double-记录
maven
复制代码
1
2
3
4
5
6<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.7</version> </dependency>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135import org.apache.commons.lang3.RandomUtils; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; /** * 按几率产生随机数 * 例如,产生0.1-100的随机数,0.1-1的几率是90%,1-10的几率是9%,10-100的几率是1% */ public class RateRandomNumber { /** * 产生随机数 * @param min 最小值 * @param max 最大值 * @return 随机结果 */ public static double produceRandomNumber(double min,double max){ return RandomUtils.nextDouble(min,max); //[min,max] } /** * 按比率产生随机数 * @param min 最小值 * @param max 最大值 * @param separates 分割值(中间插入数) * @param percents 每段数值的占比(几率) * @return 按比率随机结果 */ public static double produceRateRandomNumber(double min,double max,List<Double> separates,List<Double> percents){ if(min > max){ throw new IllegalArgumentException("min值必须小于max值"); } if(separates == null || percents==null || separates.size()==0){ return produceRandomNumber(min,max); } if(separates.size() +1 != percents.size()){ throw new IllegalArgumentException("分割数字的个数加1必须等于百分比个数"); } BigDecimal bigDecimal = new BigDecimal(0); for(Double p:percents){ if(p<0 || p>100){ throw new IllegalArgumentException("百分比必须在[0,100]之间"); } bigDecimal = bigDecimal.add(new BigDecimal(Double.toString(p))); } if(bigDecimal.doubleValue() != 100){ throw new IllegalArgumentException("百分比之和必须为100"); } for(double s:separates){ if(s <= min || s >= max){ throw new IllegalArgumentException("分割数值必须在(min,max)之间"); } } int rangeCount = separates.size()+1; //例如:3个插值,可以将一个数值范围分割成4段 //构造分割的n段范围 List<Range> ranges = new ArrayList<Range>(); double scopeMax = 0.0; for(int i=0;i<rangeCount;i++){ Range range = new Range(); range.min = (i==0 ? min:separates.get(i-1)); range.max = (i== rangeCount-1 ?max:separates.get(i)); range.percent = percents.get(i); //片段占比,转换为[1,100]区间的数字 range.percentScopeMin = scopeMax +1; range.percentScopeMax = range.percentScopeMin + (range.percent-1); scopeMax = range.percentScopeMax; ranges.add(range); } //结果赋初值 double r = min; double randomInt = RandomUtils.nextDouble(1,101); //[1,100] for(int i=0;i<ranges.size();i++){ Range range = ranges.get(i); //判断使用哪个range产生最终的随机数 if(range.percentScopeMin <= randomInt && randomInt <= range.percentScopeMax){ r = produceRandomNumber(range.min,range.max); break; } } return r; } public static class Range{ public double min; public double max; public double percent; //百分比 public double percentScopeMin; //百分比转换为[1,100]的数字的最小值 public double percentScopeMax; //百分比转换为[1,100]的数字的最大值 } public static void main(String[] args) { List<Double> separates = new ArrayList<Double>(); separates.add(1.0); separates.add(10.0); separates.add(20.0); List<Double> percents = new ArrayList<Double>(); percents.add(90.0); percents.add(5.0); percents.add(4.0); percents.add(1.0); int a = 0; int b = 0; int c = 0; int d = 0; for(int i=0;i<10000;i++) { double number = produceRateRandomNumber(0, 10000, separates, percents); if (number < 1) { a++; } if (1.0 < number && number < 10.0) { b++; } if (10.0 < number && number < 20.0) { c++; } if (20.0 < number) { d++; } System.out.println(String.format("%.2f",number)); } System.out.println(" a = " + a); System.out.println(" b = " + b); System.out.println(" c = " + c); System.out.println(" d = " + d); // System.out.println(" number = " + number); } }
最后
以上就是野性大地最近收集整理的关于简单的随机立减——JAVA实现概率计算(数字不同范围按照不同几率产生随机数)-记录的全部内容,更多相关简单内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复