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

Module:Random

From Mine in Abyss
Revision as of 13:16, 11 October 2024 by Scyu (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This module generates a pseudo-random number. It is used the same as math.random, however it first sets up a seed so that the number is actually random.


de:Modul:Zufall fr:Module:Random ru:Модуль:Случайные числа



local p = {}
local seeded
local randomseed = math.randomseed
local random = math.random

function p.random( m, n )
	if not seeded then
		p.seed()
	end
	
	return random( n and m or m and 1 or 0, n or m or 1 )
end

function p.seed( seed )
	randomseed( seed or ( os.time() + os.clock() * 1000000000 ) )
	
	-- First few values of seed is not guaranteed to be random on some platforms
	random()
	random()
	
	seeded = true
end

function p.main( frame )
	local m = tonumber( frame.args[1] )
	local n = tonumber( frame.args[2] )
	
	return tostring( p.random( n and m or m and 1 or 0, n or m or 1 ) )
end

return p