forked from joe7575/signs_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom.lua
More file actions
72 lines (57 loc) · 1.26 KB
/
random.lua
File metadata and controls
72 lines (57 loc) · 1.26 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
--[[
Signs Bot
=========
Copyright (C) 2019-2021 Joachim Stolberg
GPL v3
See LICENSE.txt for more information
Signs Bot: Random Series
]]--
-- generate a table of unique pseudo random numbers
local function gen_serie(inv_size)
local tbl = {}
local offs = inv_size/2
if offs % 2 == 1 then
offs = offs + 1
end
local index = 1
for n = 1, (inv_size*2) do
tbl[#tbl + 1] = index
index = ((index + offs) % inv_size) + 1
end
return tbl
end
-- Pseudo numbers series to iterate "randomly" through inventories
local InvRandomSeries = {}
local function get_serie(inv_size)
if not InvRandomSeries[inv_size] then
InvRandomSeries[inv_size] = gen_serie(inv_size)
end
return InvRandomSeries[inv_size]
end
local function random(inv_size)
local t = get_serie(inv_size)
local i = 0
local n = #t/2
local offs = math.random(n)
return function ()
i = i + 1
if i <= n then return t[i+offs] end
end
end
--
-- API
--
-- func signs_bot.random(inv_size)
-- random iterator providing 'inv_size' possible bot inventory stack numbers
-- inv_size must be even and between 4 and 64
signs_bot.random = random
--
-- Test
--
--for size = 4,64,2 do
-- local tbl = {}
-- for idx in random(size) do
-- tbl[#tbl + 1] = idx
-- end
-- print(table.concat(tbl, ", "))
--end