-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAliasMethod.lua
More file actions
97 lines (86 loc) · 2.33 KB
/
AliasMethod.lua
File metadata and controls
97 lines (86 loc) · 2.33 KB
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
local AliasMethod = {}
function AliasMethod.randomFromItems(items)
local largers = {}
local smallers = {}
local alias = {}
print(#items)
local total = 0
-- check toall Rate
for index, item in ipairs(items) do
total = total + item.rate
end
if (total > 1) then
print("waring: total value is more than 1")
return
end
-- items rate to 1 & devide items to large and small
local count = #items
for index, item in ipairs(items) do
local alias_item = {id = item.id, rate = item.rate * count}
if alias_item.rate < 1.0 then
table.insert(smallers, alias_item)
else
table.insert(largers, alias_item)
end
end
print("large: "..#largers.." small: "..#smallers)
-- makeup alias
while #smallers ~= 0 and #largers ~= 0 do
local small = smallers[1]
local large = largers[1]
table.remove(smallers, 1)
table.remove(largers, 1)
local expend_rate = 1.0 - small.rate;
local rest_rate = large.rate - expend_rate
if rest_rate < 0 then
print("waring: expend rate is less than 0")
break
end
local expend = {id = large.id, rate = expend_rate}
local alias_pair = {small, expend}
table.insert(alias, alias_pair)
local rest = {id = large.id, rate = rest_rate}
if rest_rate >= 1.0 then
table.insert(largers, rest)
else
table.insert(smallers, rest)
end
end
-- handle rest items
while #smallers ~= 0 do
local small = smallers[1]
table.remove(smallers, 1)
local alias_pair = {{id = small.id, rate = 1.}}
table.insert(alias, alias_pair)
end
while #largers ~= 0 do
local large = largers[1]
table.remove(largers, 1)
local alias_pair = {{id = large.id, rate = 1.}}
table.insert(alias, alias_pair)
end
print("large: "..#largers.." small: "..#smallers)
print("- ---- ----------")
for i, alias_pair in ipairs(alias) do
for i, item in ipairs(alias_pair) do
print(item.id, item.rate)
end
print("- ---- ----------")
end
local index = math.random(1, #alias)
local rate = math.random(1000) * 0.001
print("alias count: " ..count .. " random index: " ..index .." rate: " ..rate)
local alias_pair = alias[index]
local rate_range = 0
local result = nil
for i, item in ipairs(alias_pair) do
rate_range = rate_range + item.rate
if rate_range >= rate then
print("rate_range: " ..rate_range .." rate: " ..rate)
result = item
break
end
end
return result.id
end
return AliasMethod