I suggest setting a flag when the random seed is set, so it can be set just once. I know it won't help with multiple invocations of the module, since the flag w
I suggest setting a flag when the random seed is set, so it can be set just once. I know it won't help with multiple invocations of the module, since the flag won't be preserved, but at least when random numbers are being generated multiple times during a given function call, the risk of repeating the same seed value will be eliminated. isaacl (talk) 17:39, 30 November 2013 (UTC)
|same= parameter.) But I think you do have a point - it was messy code to call setRandomSeed so many times. I've reworked the module so that it is only called once. — Mr. Stradivarius ♪ talk ♪ 07:26, 2 December 2013 (UTC)
math.floor(os.time()/60) will produce different results if the time you run your module happens to coincide with a minute boundary. — Mr. Stradivarius ♪ talk ♪ 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. -- Dave Braunschweig (talk) 14:27, 30 December 2015 (UTC)
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. -- Dave Braunschweig (talk) 14:27, 30 December 2015 (UTC)
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
@Dave Braunschweig: I could use such a function so I just added it to the sandbox. Now I'll improve it to fit my needs. Thanks for the code! Sophivorus (talk) 13:15, 24 May 2020 (UTC)
If you want a separator that can contain leading or trailing spaces and don't mind never having double-quotes as part of a separator, then you can use something like
sep = sep:gsub( '"', '' )
to strip out the double quotation marks. That allows you to set a parameter such as |separator=", " in the template call. HTH --RexxS (talk) 19:11, 7 June 2018 (UTC)
"" to ". Or you could do something like replace every instance of <space> with an actual space. At the moment, you still have the option of using an HTML entity: separator =  . I'd be interested to know how much demand there is for things like this that isn't already possible with the comma separator. — Mr. Stradivarius ♪ talk ♪ 12:13, 8 June 2018 (UTC)
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.