Content deleted Content added
reply |
Comment and code sample |
||
Line 4:
::Just out of curiosity, could you point me to one of the examples where it is expected that {{tl|rand}} will produce the same number? Having a common entry point function for each of the generated wrapper function is a nice way to set the seed once. However, regarding the logic: is the logic reversed for the low traffic check? In the case where using the same seed is desired and the site is not low traffic, then stats.edit may change rapidly, so you should instead use os.time() rounded to the seed refresh rate? [[User:Isaacl|isaacl]] ([[User talk:Isaacl|talk]]) 07:42, 2 December 2013 (UTC)
:::Sure - the one I found was [https://en.wikipedia.org/w/index.php?title=Template:Random_portal_component&oldid=534588943 Template:Random portal component] (before I converted it to Lua). And no, the logic is the right way round, as mw.site.stats.edits etc. are only loaded once per page, so they are always the same. On the other hand, <code>math.floor(os.time()/60)</code> will produce different results if the time you run your module happens to coincide with a minute boundary. — '''''[[User:Mr. Stradivarius|<span style="color: #194D00; font-family: Palatino, Times, serif">Mr. Stradivarius</span>]]''''' <sup>[[User talk:Mr. Stradivarius|♪ talk ♪]]</sup> 08:08, 2 December 2013 (UTC)
I recommend adding seed as a parameter. Then when the same seed is desired, it can be provided. This allows for a very simple same-seed all day, or by hour, or minute. The default should be full randomization based on time and clock, etc. -- [[User:Dave Braunschweig|Dave Braunschweig]] ([[User talk:Dave Braunschweig|talk]]) 14:27, 30 December 2015 (UTC)
== Random Link ==
I've developed the following code for selecting a random internal link from a page, to be used for selecting featured articles from a list. But it could have other applications as well. If anyone else finds it useful, please add it to the module. -- [[User:Dave Braunschweig|Dave Braunschweig]] ([[User talk:Dave Braunschweig|talk]]) 14:27, 30 December 2015 (UTC)
<pre>
function p.link(frame)
local page = frame.args[1]
local seed = frame.args[2]
if page == nil or page == '' then
return 'Random.link: First parameter must be an existing page title.'
end
local title = mw.title.new(page)
if title.id == 0 then
return 'Random.link: First parameter must be an existing page title.'
end
if seed == nil then
math.randomseed(os.time() + math.floor(os.clock() * 1000000000))
else
math.randomseed(seed)
end
local text = title:getContent()
local links = {}
local count = 0
for link in text:gmatch("%[%[([^%]]*)%]%]") do
table.insert(links, link)
count = count + 1
end
if count == 0 then
return 'Random.link: Page has no links.'
else
local index = math.random(count)
return links[index]
end
end
</pre>
|