Module:Random

From ProleWiki, the proletarian encyclopedia
Revision as of 02:20, 18 January 2023 by Forte (talk | contribs) (Testing a better random algorithm)
local p = {}

function p.random(frame)
	local parent = frame:getParent()
	
	local a = tonumber(parent.args[1]) or 1
	local b = tonumber(parent.args[2]) or 100
	
	local Date = os.date()
	local h = Date:match("(%d+):%d+:%d+")
	local m = Date:match("%d+:(%d+):%d+")
	local s = Date:match("%d+:%d+:(%d+)")
	local rnd = s * s * math.random(1,b)
	math.randomseed(rnd)
	
	while (rnd > b or rnd < a) do
			if (rnd > b) then
				local aux = b/2
				rnd = math.floor(rnd/aux)
			elseif (rnd < a) then
				local aux = b/2
				rnd = math.floor(rnd*aux)
			end
	end
	return rnd
end
return p