Module talk:Random
Setting random seed
[edit]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)
- Actually, sometimes generating the same number is necessary, as some wikicode assumes that {{rand}} generates the same number every time it is called on the same page. (This can now be done with the
|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)- Just out of curiosity, could you point me to one of the examples where it is expected that {{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? isaacl (talk) 07:42, 2 December 2013 (UTC)
- Sure - the one I found was 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,
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)
- Sure - the one I found was 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,
- Just out of curiosity, could you point me to one of the examples where it is expected that {{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? isaacl (talk) 07:42, 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)
Random Link
[edit]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)
- This seems like too niche functionality to be added to the main Module:Random (as opposed to another module) * Pppery * it has begun... 17:35, 24 May 2020 (UTC)
Spaces in separators
[edit]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)
- @RexxS: Interesting idea. You can still allow double quotes in such a system, btw - you just have to also convert
""
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 thecomma
separator. — Mr. Stradivarius ♪ talk ♪ 12:13, 8 June 2018 (UTC)- @Mr. Stradivarius: I use it in Module:WikidataIB/sandbox (around line 444) in case somebody wants the list of returned values to have something other than ", " – perhaps a spaced dash of some sort: " – " or a spaced middot (although hlist handles that more accessibly). As you never can tell what odd applications editors will think up, I always to try to maximise the flexibility, and minimise the requirements for the users know something (e.g. html entities). Cheers --RexxS (talk) 13:23, 8 June 2018 (UTC)