Skip to content

Commit 262bf94

Browse files
committed
Fix math.random issue in lua5.1
1 parent de3353b commit 262bf94

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/prometheus.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,33 @@ end
1212
local oldPkgPath = package.path;
1313
package.path = script_path() .. "?.lua;" .. package.path;
1414

15+
-- Math.random Fix
16+
-- Check if fix is needed
17+
if not pcall(function()
18+
return math.random(1, 2^40);
19+
end) then
20+
local oldMathRandom = math.random;
21+
math.random = function(a, b)
22+
if not a and b then
23+
return oldMathRandom();
24+
end
25+
if not b then
26+
return math.random(1, a);
27+
end
28+
if a > b then
29+
a, b = b, a;
30+
end
31+
local diff = b - a;
32+
assert(diff > 0);
33+
if diff > 2 ^ 31 - 1 then
34+
return math.floor(oldMathRandom() * diff + a);
35+
else
36+
return oldMathRandom(a, b);
37+
end
38+
end
39+
end
40+
41+
1542
-- Require Prometheus Submodules
1643
local Pipeline = require("prometheus.pipeline");
1744
local highlight = require("highlightlua");

0 commit comments

Comments
 (0)