This page is directors-only protected so that only directors can edit it.

Module:DidYouKnow: Difference between revisions

Jump to navigation Jump to search
Content added Content deleted
(Add "p.getAllFacts()")
(that entire function was just setting the seed for the random function (and was an unattributed copy/port of code from http://qaru.site/questions/10188036/how-can-i-generate-dates-of-a-certain-gregorian-year-to-hijri), so just replace it with os.time())
Line 1: Line 1:
local p = {}
local p = {}
local facts = require("Module:DidYouKnow/facts")
local facts = require("Module:DidYouKnow/facts")

-- Apparently this counts days from November 3, 2018? What is that supposed to do? --ATCN
function getCurrentDay()
local now = os.date("!*t")
local day = now["day"]
local month = now["month"]
local year = now["year"]
if month < 3 then
year = year-1
month = month+12
end
local A = math.floor(year/100)
local B = math.floor(A/4)
local C = 2-A+B
local E = math.floor(365.25*(year+4716))
local F = math.floor(30.6001*(month+1))
return C+day+E+F-2459950
end


function p.getFacts()
function p.getFacts()
local s = ""
local s = ""
local length = #facts.facts
local length = #facts.facts
math.randomseed(getCurrentDay())
math.randomseed(os.time())
[[
os.time() only has second-level resolution, not millisecond-level, so we
may end up with the same seed sometimes
since all we're doing is picking random facts from a list, this isn't
a big deal
]]
for i = 0, 4 do
for i = 0, 4 do

Revision as of 22:37, 16 December 2018

Implements {{DidYouKnow}}. All individual hooks are stored and can be added at Module:DidYouKnow/facts.

[view] [edit] [history] [refresh]The above documentation is transcluded from Module:DidYouKnow/doc.
local p = {}
local facts = require("Module:DidYouKnow/facts")

function p.getFacts()
	local s = ""
	local length = #facts.facts
	math.randomseed(os.time())
		[[
		os.time() only has second-level resolution, not millisecond-level, so we
		may end up with the same seed sometimes
		since all we're doing is picking random facts from a list, this isn't
		a big deal
		]]
	
	for i = 0, 4 do
		s = s .. "* ... that " .. facts.facts[math.random(1, length)] .. "?"
		if i < 4 then s = s .. "\n" end
	end
	return s
end

-- List all facts. Intended to allow easier debugging.
function p.getAllFacts()
	local result = {}
	local length = #facts.facts
	
	for i = 1, length do
		result[i] = "* ... that " .. facts.facts[i] .. "?"
	end
	return table.concat(result, "\n")
end

return p