Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Random: Difference between revisions

From ProleWiki, the proletarian encyclopedia
m (Attempt to improve random number generator)
m (Loop prevent fallback, added fallback aux variable to make calculation easier for lower numbers)
 
(14 intermediate revisions by the same user not shown)
Line 11: Line 11:
local m = Date:match("%d+:(%d+):%d+")
local m = Date:match("%d+:(%d+):%d+")
local s = Date:match("%d+:%d+:(%d+)")
local s = Date:match("%d+:%d+:(%d+)")
local rnd = s * s * math.random(1,10000)
local rnd = s * s * math.random(a,b)
while (rnd > b) do
local loop = 0
rnd = math.floor(rnd/b)
while (rnd > b or rnd < a) do
end
loop = loop +1
while (rnd < a) do
if (loop > 50) then
rnd = math.floor(rnd*1.5)
rnd = a
break
end
if (rnd > b) then
local aux = b/2
if (aux < 1) then
aux = 2.5
end
rnd = math.floor(rnd/aux)
elseif (rnd < a) then
local aux = b/2
if (aux < 1) then
aux = 2
end
rnd = math.floor(rnd*aux)
end
math.randomseed(rnd)
end
end
return rnd
return rnd
end
end
return p
return p

Latest revision as of 02:42, 18 January 2023

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(a,b)
	
	local loop = 0
	while (rnd > b or rnd < a) do
			loop = loop +1
			if (loop > 50) then
				rnd = a
				break
			end
			if (rnd > b) then
				local aux = b/2
				if (aux < 1) then
					aux = 2.5
				end
				rnd = math.floor(rnd/aux)
			elseif (rnd < a) then
				local aux = b/2
				if (aux < 1) then
					aux = 2
				end
				rnd = math.floor(rnd*aux)
			end
			math.randomseed(rnd)
	end
	return rnd
end
return p