Toggle menu
14
232
69
27.2K
Kenshi Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.
Revision as of 00:13, 23 February 2025 by Prd (talk | contribs) (Created page with "local p = {}; local cfg = {} -------------------------------------------------------------------------------------- -- Configuration -------------------------------------------------------------------------------------- -- Set this to true if your wiki has a traffic rate of less than one edit every two minutes or so. -- This will prevent the same "random" number being generated many times in a row until a new edit is made -- to the wiki. This setting is only relevant i...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Implements {{Choice}}



local p = {};
local cfg = {}

--------------------------------------------------------------------------------------
-- Configuration
--------------------------------------------------------------------------------------

-- Set this to true if your wiki has a traffic rate of less than one edit every two minutes or so.
-- This will prevent the same "random" number being generated many times in a row until a new edit is made
-- to the wiki. This setting is only relevant if the |same= parameter is set.
cfg.lowTraffic = false

-- If cfg.lowTraffic is set to true, and the |same= parameter is set, this value is used for the refresh rate of the random seed.
-- This is the number of seconds until the seed is changed. Getting this right is tricky. If you set it too high, the same number
-- will be returned many times in a row. If you set it too low, you may get different random numbers appearing on the same page,
-- particularly for pages that take many seconds to process.
cfg.seedRefreshRate = 60

local function deepargs(args)
	local nice_args_table = {}
	for k, v in pairs(args) do
		if v and v ~= '' then
			nice_args_table[k] = v
		end
	end
	return nice_args_table
end

p._choice = function( args )
    args = deepargs(args)
    local res = {};
    local n;
    local lim = tonumber(args[1])
    local separator = args[2]
    table.remove(args,1);
    table.remove(args,1);
    for i = 1,lim,1 do
        n = math.random( #args );
        table.insert(res,args[n]);
        table.remove(args,n);
    end
    return table.concat(res,separator);
end


p.choice = function( frame )     --Add a function to "p".
    if not cfg.lowTraffic then
		-- Make the seed as random as possible without using anything time-based. This means that the same random number
		-- will be generated for the same input from the same page - necessary behaviour for some wikicode templates that
		-- assume bad pseudo-random-number generation.
		local stats = mw.site.stats
		local views = stats.views or 0 -- This is not always available, so we need a backup.
		local seed = views + stats.pages + stats.articles + stats.files + stats.edits + stats.users + stats.activeUsers + stats.admins -- Make this as random as possible without using os.time() or os.clock()
		math.randomseed(seed)
	else
		-- Make the random seed change every n seconds, where n is set by cfg.seedRefreshRate.
		-- This is useful for low-traffic wikis where new edits may not happen very often.
		math.randomseed(math.floor(os.time() / cfg.seedRefreshRate))
    end
    local args = frame:getParent().args;
    return p._choice(args)
end
return p;