mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-22 07:13:45 +01:00
Add math.rand(om|int)
This commit is contained in:
parent
93c1338135
commit
d94092ae9f
48
math.lua
48
math.lua
@ -1,6 +1,6 @@
|
|||||||
-- Localize globals
|
-- Localize globals
|
||||||
local math, math_floor, minetest, modlib_table_reverse, os, string_char, setmetatable, table_insert, table_concat
|
local assert, math, math_floor, minetest, modlib_table_reverse, os, string_char, select, setmetatable, table_insert, table_concat
|
||||||
= math, math.floor, minetest, modlib.table.reverse, os, string.char, setmetatable, table.insert, table.concat
|
= assert, math, math.floor, minetest, modlib.table.reverse, os, string.char, select, setmetatable, table.insert, table.concat
|
||||||
|
|
||||||
local inf = math.huge
|
local inf = math.huge
|
||||||
|
|
||||||
@ -27,6 +27,50 @@ function clamp(number, min, max)
|
|||||||
return math.min(math.max(number, min), max)
|
return math.min(math.max(number, min), max)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Random integer from 0 to 2^53 - 1 (inclusive)
|
||||||
|
local function _randint()
|
||||||
|
return math.random(0, 2^27 - 1) * 2^26 + math.random(0, 2^26 - 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Random float from 0 to 1 (exclusive)
|
||||||
|
local function _randfloat()
|
||||||
|
return _randint() / (2^53)
|
||||||
|
end
|
||||||
|
|
||||||
|
--+ Increased randomness float random without overflows
|
||||||
|
--+ `random()`: Random number from `0` to `1` (exclusive)
|
||||||
|
--+ `random(max)`: Random number from `0` to `max` (exclusive)
|
||||||
|
--+ `random(min, max)`: Random number from `min` to `max` (exclusive)
|
||||||
|
function random(...)
|
||||||
|
local n = select("#", ...)
|
||||||
|
if n == 0 then
|
||||||
|
return _randfloat()
|
||||||
|
end if n == 1 then
|
||||||
|
local max = ...
|
||||||
|
return _randfloat() * max
|
||||||
|
end do assert(n == 2)
|
||||||
|
local min, max = ...
|
||||||
|
return min + (max - min) * _randfloat()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Increased randomness integer random
|
||||||
|
--+ `randint()`: Random integer from `0` to `2^53 - 1` (inclusive)
|
||||||
|
--+ `randint(max)`: Random integer from `0` to `max` (inclusive)
|
||||||
|
--+ `randint(min, max)`: Random integer from `min` to `max` (inclusive)
|
||||||
|
function randint(...)
|
||||||
|
local n = select("#", ...)
|
||||||
|
if n == 0 then
|
||||||
|
return _randint()
|
||||||
|
end if n == 1 then
|
||||||
|
local max = ...
|
||||||
|
return math.floor(_randfloat() * max + 0.5)
|
||||||
|
end do assert(n == 2)
|
||||||
|
local min, max = ...
|
||||||
|
return min + math.floor(_randfloat() * (max - min) + 0.5)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
log = setmetatable({}, {
|
log = setmetatable({}, {
|
||||||
__index = function(self, base)
|
__index = function(self, base)
|
||||||
local div = math.log(base)
|
local div = math.log(base)
|
||||||
|
Loading…
Reference in New Issue
Block a user