Module:Sandbox/trappist the monk/random sort

require('strict');
local content = mw.title.getCurrentTitle():getContent() or '';					-- get the content of the list page

--[[--------------------------< R A N D O M _ S O R T >--------------------------------------------------------

swaps two members of a sequence table.  One member is indexed sequentially (starting at [1]), the other index
is randomly selected.

]]

local function random_sort (frame)
	local source = setmetatable({}, {__index = table})
	local r_idx;

	for article in content:gmatch ('([^\r\n]+)[\r\n]+') do						-- get an article title
		if not (
			article:match ('<!%-%-') or 										-- skip any line that has opening comment
			article:match ('%-%->') or											-- skip any line that has closing comment
			article:match ('{{') or												-- skip the line that holds the {{#invoke:}} for this function
			'' == article) then	-- skip these									-- skip empty lines
				source:insert (article);										-- all others, add to the list
		end
	end

	math.randomseed (os.time());												-- init random number generator with current timestamp

	for i, v in ipairs (source) do
		r_idx = math.random (i, #source)										-- random index between i and length of source{} ([[Fisher–Yates shuffle]] per Anomie at my talk page)

		if i ~= r_idx then														-- if i and r_idx happen to be the same, don't bother swapping this article title with itself
			source[i], source[r_idx] = source[r_idx], source[i];				-- swap article titles source[i] and source[r_idx]
		end
	end

	return table.concat ({'count = ', #source, '<br />',
		frame:callParserFunction ('#tag:syntaxhighlight', source:concat ('\n'))});
end


--[[--------------------------< E X P O R T E D   F U N C T I O N----------------------------------------------
]]

return {
	random_sort = random_sort
	}

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.