序号 | 物品名称 | 物品ID | 抽奖概率 |
1 | 物品1 | P1 | 0.2 |
2 | 物品2 | P2 | 0.1 |
3 | 物品3 | P3 | 0.4 |
4 | 物品4 | P4 | 0.3 |
5 | 物品5 | P5 | 0.0 |
6 | 物品6 | P6 | -0.1 |
7 | 物品7 | P7 | 0.008 |
- VO类:Gift.java 具体对应就是上面表格里的内容
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
|
public
class
Gift {
private
int
index;
private
String gitfId;
private
String giftName;
private
double
probability;
public
Gift(
int
index, String gitfId, String giftName,
double
probability) {
this
.index = index;
this
.gitfId = gitfId;
this
.giftName = giftName;
this
.probability = probability;
}
public
int
getIndex() {
return
index;
}
public
void
setIndex(
int
index) {
this
.index = index;
}
public
String getGitfId() {
return
gitfId;
}
public
void
setGitfId(String gitfId) {
this
.gitfId = gitfId;
}
public
String getGiftName() {
return
giftName;
}
public
void
setGiftName(String giftName) {
this
.giftName = giftName;
}
public
double
getProbability() {
return
probability;
}
public
void
setProbability(
double
probability) {
this
.probability = probability;
}
@Override
public
String toString() {
return
"Gift [index="
+ index +
", gitfId="
+ gitfId +
", giftName="
+ giftName +
", probability="
+ probability +
"]"
;
}
}
|
- Util类:LotteryUtil.java 真正的不同概率的抽奖工具类
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
|
import
java.util.ArrayList;
import
java.util.Collections;
import
java.util.List;
/**
* 不同概率抽奖工具包
*
* @author Shunli
*/
public
class
LotteryUtil {
/**
* 抽奖
*
* @param orignalRates
* 原始的概率列表,保证顺序和实际物品对应
* @return
* 物品的索引
*/
public
static
int
lottery(List<Double> orignalRates) {
if
(orignalRates ==
null
|| orignalRates.isEmpty()) {
return
-
1
;
}
int
size = orignalRates.size();
// 计算总概率,这样可以保证不一定总概率是1
double
sumRate = 0d;
for
(
double
rate : orignalRates) {
sumRate += rate;
}
// 计算每个物品在总概率的基础下的概率情况
List<Double> sortOrignalRates =
new
ArrayList<Double>(size);
Double tempSumRate = 0d;
for
(
double
rate : orignalRates) {
tempSumRate += rate;
sortOrignalRates.add(tempSumRate / sumRate);
}
// 根据区块值来获取抽取到的物品索引
double
nextDouble = Math.random();
sortOrignalRates.add(nextDouble);
Collections.sort(sortOrignalRates);
return
sortOrignalRates.indexOf(nextDouble);
}
}
|
- Test类:Test.java
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
|
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.List;
import
java.util.Map;
import
java.util.Map.Entry;
/**
* 不同概率抽奖
*
* @author ShunLi
*/
public
class
LotteryTest {
public
static
void
main(String[] args) {
List<Gift> gifts =
new
ArrayList<Gift>();
// 序号==物品Id==物品名称==概率
gifts.add(
new
Gift(
1
,
"P1"
,
"物品1"
,
0
.2d));
gifts.add(
new
Gift(
2
,
"P2"
,
"物品2"
,
0
.2d));
gifts.add(
new
Gift(
3
,
"P3"
,
"物品3"
,
0
.4d));
gifts.add(
new
Gift(
4
,
"P4"
,
"物品4"
,
0
.3d));
gifts.add(
new
Gift(
5
,
"P5"
,
"物品5"
, 0d));
gifts.add(
new
Gift(
6
,
"P6"
,
"物品6"
, -
0
.1d));
gifts.add(
new
Gift(
7
,
"P7"
,
"物品7"
,
0
.008d));
List<Double> orignalRates =
new
ArrayList<Double>(gifts.size());
for
(Gift gift : gifts) {
double
probability = gift.getProbability();
if
(probability <
0
) {
probability =
0
;
}
orignalRates.add(probability);
}
// statistics
Map<Integer, Integer> count =
new
HashMap<Integer, Integer>();
double
num =
1000000
;
for
(
int
i =
0
; i < num; i++) {
int
orignalIndex = LotteryUtil.lottery(orignalRates);
Integer value = count.get(orignalIndex);
count.put(orignalIndex, value ==
null
?
1
: value +
1
);
}
for
(Entry<Integer, Integer> entry : count.entrySet()) {
System.out.println(gifts.get(entry.getKey()) +
", count="
+ entry.getValue() +
", probability="
+ entry.getValue() / num);
}
}
}
|
不同概率的抽奖原理很简单
就是把0到1的区间分块,而分块的依据就是物品占整个的比重,再根据随机数种子来产生0-1中间的某个数,来判断这个数是落在哪个区间上,而对应的就是抽到了那个物品。随机数理论上是概率均等的,产生的每个数理论上也应该概率均等,那么相应的区间所含数的多少就体现了抽奖物品概率的不同。(p.s. 当然数目是数不清楚的,具体抽象话了点)
这个实例的数据可以说明
1. 概率可以是负数和0,当然实际上中应该不会(p.s. 正常情况下可能真的有0,比如抽个iphone5,当然是抽不到的了,这个时候,构建礼物(List gifts)的时候最好就不要加这个进去),还有可以把负数的处理放到抽奖工具类(LotteryUtil)中;
2. 所有礼物加起来的概率可以不是1,可以认为这里的概率是一个权重;
http://my.oschina.net/u/1026723/blog/271650
序号 | 物品名称 | 物品ID | 抽奖概率 |
1 | 物品1 | P1 | 0.2 |
2 | 物品2 | P2 | 0.1 |
3 | 物品3 | P3 | 0.4 |
4 | 物品4 | P4 | 0.3 |
5 | 物品5 | P5 | 0.0 |
6 | 物品6 | P6 | -0.1 |
7 | 物品7 | P7 | 0.008 |
- VO类:Gift.java 具体对应就是上面表格里的内容
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
|
public
class
Gift {
private
int
index;
private
String gitfId;
private
String giftName;
private
double
probability;
public
Gift(
int
index, String gitfId, String giftName,
double
probability) {
this
.index = index;
this
.gitfId = gitfId;
this
.giftName = giftName;
this
.probability = probability;
}
public
int
getIndex() {
return
index;
}
public
void
setIndex(
int
index) {
this
.index = index;
}
public
String getGitfId() {
return
gitfId;
}
public
void
setGitfId(String gitfId) {
this
.gitfId = gitfId;
}
public
String getGiftName() {
return
giftName;
}
public
void
setGiftName(String giftName) {
this
.giftName = giftName;
}
public
double
getProbability() {
return
probability;
}
public
void
setProbability(
double
probability) {
this
.probability = probability;
}
@Override
public
String toString() {
return
"Gift [index="
+ index +
", gitfId="
+ gitfId +
", giftName="
+ giftName +
", probability="
+ probability +
"]"
;
}
}
|
- Util类:LotteryUtil.java 真正的不同概率的抽奖工具类
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
|
import
java.util.ArrayList;
import
java.util.Collections;
import
java.util.List;
/**
* 不同概率抽奖工具包
*
* @author Shunli
*/
public
class
LotteryUtil {
/**
* 抽奖
*
* @param orignalRates
* 原始的概率列表,保证顺序和实际物品对应
* @return
* 物品的索引
*/
public
static
int
lottery(List<Double> orignalRates) {
if
(orignalRates ==
null
|| orignalRates.isEmpty()) {
return
-
1
;
}
int
size = orignalRates.size();
// 计算总概率,这样可以保证不一定总概率是1
double
sumRate = 0d;
for
(
double
rate : orignalRates) {
sumRate += rate;
}
// 计算每个物品在总概率的基础下的概率情况
List<Double> sortOrignalRates =
new
ArrayList<Double>(size);
Double tempSumRate = 0d;
for
(
double
rate : orignalRates) {
tempSumRate += rate;
sortOrignalRates.add(tempSumRate / sumRate);
}
// 根据区块值来获取抽取到的物品索引
double
nextDouble = Math.random();
sortOrignalRates.add(nextDouble);
Collections.sort(sortOrignalRates);
return
sortOrignalRates.indexOf(nextDouble);
}
}
|
- Test类:Test.java
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
|
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.List;
import
java.util.Map;
import
java.util.Map.Entry;
/**
* 不同概率抽奖
*
* @author ShunLi
*/
public
class
LotteryTest {
public
static
void
main(String[] args) {
List<Gift> gifts =
new
ArrayList<Gift>();
// 序号==物品Id==物品名称==概率
gifts.add(
new
Gift(
1
,
"P1"
,
"物品1"
,
0
.2d));
gifts.add(
new
Gift(
2
,
"P2"
,
"物品2"
,
0
.2d));
gifts.add(
new
Gift(
3
,
"P3"
,
"物品3"
,
0
.4d));
gifts.add(
new
Gift(
4
,
"P4"
,
"物品4"
,
0
.3d));
gifts.add(
new
Gift(
5
,
"P5"
,
"物品5"
, 0d));
gifts.add(
new
Gift(
6
,
"P6"
,
"物品6"
, -
0
.1d));
gifts.add(
new
Gift(
7
,
"P7"
,
"物品7"
,
0
.008d));
List<Double> orignalRates =
new
ArrayList<Double>(gifts.size());
for
(Gift gift : gifts) {
double
probability = gift.getProbability();
if
(probability <
0
) {
probability =
0
;
}
orignalRates.add(probability);
}
// statistics
Map<Integer, Integer> count =
new
HashMap<Integer, Integer>();
double
num =
1000000
;
for
(
int
i =
0
; i < num; i++) {
int
orignalIndex = LotteryUtil.lottery(orignalRates);
Integer value = count.get(orignalIndex);
count.put(orignalIndex, value ==
null
?
1
: value +
1
);
}
for
(Entry<Integer, Integer> entry : count.entrySet()) {
System.out.println(gifts.get(entry.getKey()) +
", count="
+ entry.getValue() +
", probability="
+ entry.getValue() / num);
}
}
}
|
不同概率的抽奖原理很简单
就是把0到1的区间分块,而分块的依据就是物品占整个的比重,再根据随机数种子来产生0-1中间的某个数,来判断这个数是落在哪个区间上,而对应的就是抽到了那个物品。随机数理论上是概率均等的,产生的每个数理论上也应该概率均等,那么相应的区间所含数的多少就体现了抽奖物品概率的不同。(p.s. 当然数目是数不清楚的,具体抽象话了点)
这个实例的数据可以说明
1. 概率可以是负数和0,当然实际上中应该不会(p.s. 正常情况下可能真的有0,比如抽个iphone5,当然是抽不到的了,这个时候,构建礼物(List gifts)的时候最好就不要加这个进去),还有可以把负数的处理放到抽奖工具类(LotteryUtil)中;
2. 所有礼物加起来的概率可以不是1,可以认为这里的概率是一个权重;
序号 | 物品名称 | 物品ID | 抽奖概率 |
1 | 物品1 | P1 | 0.2 |
2 | 物品2 | P2 | 0.1 |
3 | 物品3 | P3 | 0.4 |
4 | 物品4 | P4 | 0.3 |
5 | 物品5 | P5 | 0.0 |
6 | 物品6 | P6 | -0.1 |
7 | 物品7 | P7 | 0.008 |
- VO类:Gift.java 具体对应就是上面表格里的内容
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
|
public
class
Gift {
private
int
index;
private
String gitfId;
private
String giftName;
private
double
probability;
public
Gift(
int
index, String gitfId, String giftName,
double
probability) {
this
.index = index;
this
.gitfId = gitfId;
this
.giftName = giftName;
this
.probability = probability;
}
public
int
getIndex() {
return
index;
}
public
void
setIndex(
int
index) {
this
.index = index;
}
public
String getGitfId() {
return
gitfId;
}
public
void
setGitfId(String gitfId) {
this
.gitfId = gitfId;
}
public
String getGiftName() {
return
giftName;
}
public
void
setGiftName(String giftName) {
this
.giftName = giftName;
}
public
double
getProbability() {
return
probability;
}
public
void
setProbability(
double
probability) {
this
.probability = probability;
}
@Override
public
String toString() {
return
"Gift [index="
+ index +
", gitfId="
+ gitfId +
", giftName="
+ giftName +
", probability="
+ probability +
"]"
;
}
}
|
- Util类:LotteryUtil.java 真正的不同概率的抽奖工具类
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
|
import
java.util.ArrayList;
import
java.util.Collections;
import
java.util.List;
/**
* 不同概率抽奖工具包
*
* @author Shunli
*/
public
class
LotteryUtil {
/**
* 抽奖
*
* @param orignalRates
* 原始的概率列表,保证顺序和实际物品对应
* @return
* 物品的索引
*/
public
static
int
lottery(List<Double> orignalRates) {
if
(orignalRates ==
null
|| orignalRates.isEmpty()) {
return
-
1
;
}
int
size = orignalRates.size();
// 计算总概率,这样可以保证不一定总概率是1
double
sumRate = 0d;
for
(
double
rate : orignalRates) {
sumRate += rate;
}
// 计算每个物品在总概率的基础下的概率情况
List<Double> sortOrignalRates =
new
ArrayList<Double>(size);
Double tempSumRate = 0d;
for
(
double
rate : orignalRates) {
tempSumRate += rate;
sortOrignalRates.add(tempSumRate / sumRate);
}
// 根据区块值来获取抽取到的物品索引
double
nextDouble = Math.random();
sortOrignalRates.add(nextDouble);
Collections.sort(sortOrignalRates);
return
sortOrignalRates.indexOf(nextDouble);
}
}
|
- Test类:Test.java
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
|
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.List;
import
java.util.Map;
import
java.util.Map.Entry;
/**
* 不同概率抽奖
*
* @author ShunLi
*/
public
class
LotteryTest {
public
static
void
main(String[] args) {
List<Gift> gifts =
new
ArrayList<Gift>();
// 序号==物品Id==物品名称==概率
gifts.add(
new
Gift(
1
,
"P1"
,
"物品1"
,
0
.2d));
gifts.add(
new
Gift(
2
,
"P2"
,
"物品2"
,
0
.2d));
gifts.add(
new
Gift(
3
,
"P3"
,
"物品3"
,
0
.4d));
gifts.add(
new
Gift(
4
,
"P4"
,
"物品4"
,
0
.3d));
gifts.add(
new
Gift(
5
,
"P5"
,
"物品5"
, 0d));
gifts.add(
new
Gift(
6
,
"P6"
,
"物品6"
, -
0
.1d));
gifts.add(
new
Gift(
7
,
"P7"
,
"物品7"
,
0
.008d));
List<Double> orignalRates =
new
ArrayList<Double>(gifts.size());
for
(Gift gift : gifts) {
double
probability = gift.getProbability();
if
(probability <
0
) {
probability =
0
;
}
orignalRates.add(probability);
}
// statistics
Map<Integer, Integer> count =
new
HashMap<Integer, Integer>();
double
num =
1000000
;
for
(
int
i =
0
; i < num; i++) {
int
orignalIndex = LotteryUtil.lottery(orignalRates);
Integer value = count.get(orignalIndex);
count.put(orignalIndex, value ==
null
?
1
: value +
1
);
}
for
(Entry<Integer, Integer> entry : count.entrySet()) {
System.out.println(gifts.get(entry.getKey()) +
", count="
+ entry.getValue() +
", probability="
+ entry.getValue() / num);
}
}
}
|
不同概率的抽奖原理很简单
就是把0到1的区间分块,而分块的依据就是物品占整个的比重,再根据随机数种子来产生0-1中间的某个数,来判断这个数是落在哪个区间上,而对应的就是抽到了那个物品。随机数理论上是概率均等的,产生的每个数理论上也应该概率均等,那么相应的区间所含数的多少就体现了抽奖物品概率的不同。(p.s. 当然数目是数不清楚的,具体抽象话了点)
这个实例的数据可以说明
1. 概率可以是负数和0,当然实际上中应该不会(p.s. 正常情况下可能真的有0,比如抽个iphone5,当然是抽不到的了,这个时候,构建礼物(List gifts)的时候最好就不要加这个进去),还有可以把负数的处理放到抽奖工具类(LotteryUtil)中;
2. 所有礼物加起来的概率可以不是1,可以认为这里的概率是一个权重;
序号 | 物品名称 | 物品ID | 抽奖概率 |
1 | 物品1 | P1 | 0.2 |
2 | 物品2 | P2 | 0.1 |
3 | 物品3 | P3 | 0.4 |
4 | 物品4 | P4 | 0.3 |
5 | 物品5 | P5 | 0.0 |
6 | 物品6 | P6 | -0.1 |
7 | 物品7 | P7 | 0.008 |
- VO类:Gift.java 具体对应就是上面表格里的内容
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
|
public
class
Gift {
private
int
index;
private
String gitfId;
private
String giftName;
private
double
probability;
public
Gift(
int
index, String gitfId, String giftName,
double
probability) {
this
.index = index;
this
.gitfId = gitfId;
this
.giftName = giftName;
this
.probability = probability;
}
public
int
getIndex() {
return
index;
}
public
void
setIndex(
int
index) {
this
.index = index;
}
public
String getGitfId() {
return
gitfId;
}
public
void
setGitfId(String gitfId) {
this
.gitfId = gitfId;
}
public
String getGiftName() {
return
giftName;
}
public
void
setGiftName(String giftName) {
this
.giftName = giftName;
}
public
double
getProbability() {
return
probability;
}
public
void
setProbability(
double
probability) {
this
.probability = probability;
}
@Override
public
String toString() {
return
"Gift [index="
+ index +
", gitfId="
+ gitfId +
", giftName="
+ giftName +
", probability="
+ probability +
"]"
;
}
}
|
- Util类:LotteryUtil.java 真正的不同概率的抽奖工具类
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
|
import
java.util.ArrayList;
import
java.util.Collections;
import
java.util.List;
/**
* 不同概率抽奖工具包
*
* @author Shunli
*/
public
class
LotteryUtil {
/**
* 抽奖
*
* @param orignalRates
* 原始的概率列表,保证顺序和实际物品对应
* @return
* 物品的索引
*/
public
static
int
lottery(List<Double> orignalRates) {
if
(orignalRates ==
null
|| orignalRates.isEmpty()) {
return
-
1
;
}
int
size = orignalRates.size();
// 计算总概率,这样可以保证不一定总概率是1
double
sumRate = 0d;
for
(
double
rate : orignalRates) {
sumRate += rate;
}
// 计算每个物品在总概率的基础下的概率情况
List<Double> sortOrignalRates =
new
ArrayList<Double>(size);
Double tempSumRate = 0d;
for
(
double
rate : orignalRates) {
tempSumRate += rate;
sortOrignalRates.add(tempSumRate / sumRate);
}
// 根据区块值来获取抽取到的物品索引
double
nextDouble = Math.random();
sortOrignalRates.add(nextDouble);
Collections.sort(sortOrignalRates);
return
sortOrignalRates.indexOf(nextDouble);
}
}
|
- Test类:Test.java
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
|
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.List;
import
java.util.Map;
import
java.util.Map.Entry;
/**
* 不同概率抽奖
*
* @author ShunLi
*/
public
class
LotteryTest {
public
static
void
main(String[] args) {
List<Gift> gifts =
new
ArrayList<Gift>();
// 序号==物品Id==物品名称==概率
gifts.add(
new
Gift(
1
,
"P1"
,
"物品1"
,
0
.2d));
gifts.add(
new
Gift(
2
,
"P2"
,
"物品2"
,
0
.2d));
gifts.add(
new
Gift(
3
,
"P3"
,
"物品3"
,
0
.4d));
gifts.add(
new
Gift(
4
,
"P4"
,
"物品4"
,
0
.3d));
gifts.add(
new
Gift(
5
,
"P5"
,
"物品5"
, 0d));
gifts.add(
new
Gift(
6
,
"P6"
,
"物品6"
, -
0
.1d));
gifts.add(
new
Gift(
7
,
"P7"
,
"物品7"
,
0
.008d));
List<Double> orignalRates =
new
ArrayList<Double>(gifts.size());
for
(Gift gift : gifts) {
double
probability = gift.getProbability();
if
(probability <
0
) {
probability =
0
;
}
orignalRates.add(probability);
}
// statistics
Map<Integer, Integer> count =
new
HashMap<Integer, Integer>();
double
num =
1000000
;
for
(
int
i =
0
; i < num; i++) {
int
orignalIndex = LotteryUtil.lottery(orignalRates);
Integer value = count.get(orignalIndex);
count.put(orignalIndex, value ==
null
?
1
: value +
1
);
}
for
(Entry<Integer, Integer> entry : count.entrySet()) {
System.out.println(gifts.get(entry.getKey()) +
", count="
+ entry.getValue() +
", probability="
+ entry.getValue() / num);
}
}
}
|
不同概率的抽奖原理很简单
就是把0到1的区间分块,而分块的依据就是物品占整个的比重,再根据随机数种子来产生0-1中间的某个数,来判断这个数是落在哪个区间上,而对应的就是抽到了那个物品。随机数理论上是概率均等的,产生的每个数理论上也应该概率均等,那么相应的区间所含数的多少就体现了抽奖物品概率的不同。(p.s. 当然数目是数不清楚的,具体抽象话了点)
这个实例的数据可以说明
1. 概率可以是负数和0,当然实际上中应该不会(p.s. 正常情况下可能真的有0,比如抽个iphone5,当然是抽不到的了,这个时候,构建礼物(List gifts)的时候最好就不要加这个进去),还有可以把负数的处理放到抽奖工具类(LotteryUtil)中;
2. 所有礼物加起来的概率可以不是1,可以认为这里的概率是一个权重;