Content deleted Content added
tidy indents and remove confusing left-over comment |
add a function that gets the same random number on every page. |
||
Line 4:
local makeList = require('Module:List').makeList
-- Set the seed for the random number generator. This works well on the English Wikipedia due to the high▼
-- edit rate, but should also work well on smaller wikis due to the variability of os.time() (the current time)▼
-- and os.clock() (the time the program takes to run).▼
math.randomseed(mw.site.stats.edits + mw.site.stats.pages + os.time() + math.floor(os.clock() * 1000000000))▼
-- Call math.random a few times to avoid getting the same first answer every time.▼
math.random()▼
math.random()▼
math.random()▼
--------------------------------------------------------------------------------------
-- Helper functions
--------------------------------------------------------------------------------------
local function setRandomSeed()
▲ -- Set the seed for the random number generator. This works well on the English Wikipedia due to the high
▲ -- edit rate, but should also work well on smaller wikis due to the variability of os.time() (the current time)
▲ -- and os.clock() (the time the program takes to run).
▲ math.randomseed(mw.site.stats.edits + mw.site.stats.pages + os.time() + math.floor(os.clock() * 1000000000))
▲ -- Call math.random a few times to avoid getting the same first answer every time.
▲ math.random()
▲ math.random()
▲ math.random()
end
local function raiseError(msg)
Line 28 ⟶ 30:
--------------------------------------------------------------------------------------
local function
-- Gets a random integer between l and u, and is not limited to RAND_MAX (which here we assume is 2^31-1).
local r = math.random() + math.random() / 2147483648
Line 34 ⟶ 36:
end
local function
--
first = tonumber(args[1])
second = tonumber(args[2])
Line 44 ⟶ 46:
first, second = second, first
end
return
else
return
end
else
return math.random()
end
end
function p._number(args)
-- Returns a random number. Will return different random numbers even on the same page.
setRandomSeed()
return getRandom(args)
end
function p._same_number(args)
-- Returns a random number. Will return the same random number on the same page if called with the same parameters.
-- If you are using a very low traffic wiki this might not update frequently enough, and you may get the same "random"
-- number many times in a row. (Until someone makes a new edit, or creates a new user account, etc.)
-- To fix this, replace the line beginning with "local seed" with the following code:
--
-- local seed = math.floor(os.time() % 60)
--
-- This makes the seed change once a minute. (The "60" is the number of seconds between one seed and the next.)
-- You can decrease the value if you want the seed to update more frequently. However, smaller values increase the risk
-- that the function may return different values, especially for calls to Lua that take several seconds to complete.
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)
return getRandom(args)
end
Line 65 ⟶ 91:
-- The default format is the standard Wikipedia timestamp.
setRandomSeed()
local lang = mw.language.getContentLanguage()
Line 113 ⟶ 140:
-- Get a random number between the two Unix timestamps and return it using the specified format.
local randomTimestamp =
local dateFormat = args.format or 'H:i, d F Y (T)'
local result = getDate(dateFormat, '@' .. tostring(randomTimestamp))
Line 130 ⟶ 157:
-- Randomizes an array. It works by iterating through the list backwards, each time swapping the entry
-- "i" with a random entry. Courtesy of Xinhuan at http://forums.wowace.com/showthread.php?p=279756
setRandomSeed()
for i = #t, 2, -1 do
local r = math.random(i)
Line 176 ⟶ 204:
function p._item(args)
-- Returns a random item from a numbered list.
setRandomSeed()
local list = removeBlanks(args)
if #list >= 1 then
Line 184 ⟶ 213:
function p._list(args)
-- Randomizes a list and concatenates the result with a separator.
setRandomSeed()
local list = makeRandomList(args)
local sep = makeSeparator(args.sep or args.separator)
Line 191 ⟶ 221:
function p._text_list(args)
-- Randomizes a list and concatenates the result, text-style. Accepts separator and conjunction arguments.
setRandomSeed()
local list = makeRandomList(args)
local sep = makeSeparator(args.sep or args.separator)
Line 203 ⟶ 234:
function p.html_list(args, listType)
-- Randomizes a list and turns it into an HTML list. Uses [[Module:List]].
setRandomSeed()
listType = listType or 'bulleted'
local listArgs = makeRandomList(args) -- Arguments for [[Module:List]].
Line 260 ⟶ 292:
-- Process arguments for other functions.
local otherFuncs = {'number', 'same_number', 'date', 'item', 'list', 'text_list'}
for _, funcName in ipairs(otherFuncs) do
p[funcName] = makeWrapper('_' .. funcName)
|