Sample (random) filter operation

157 views
Skip to first unread message

Devin Weaver

unread,
Sep 9, 2019, 4:05:43 PM9/9/19
to TiddlyWiki
For anyone curious here is a neat filter option to grab a random tiddler from a list:

<$list filter="[tag[quote]sample[3]]">
<$transclude/>
</$list>

will show you three random tiddlers with the quote tag.

/*\
title: $:/filters/sample.js
type: application/javascript
module-type: filteroperator

Filter to return random set from the current list.

\*/

(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

/*
Export our filter function
*/

exports
.sample = function(source,operator,options) {
 
var sampleIndex;
 
var allTitles = [];
 
var samples = [];
 
var numberOfSamples = parseInt(operator.operand) || 1;
  source
(function(tiddler,title) { allTitles.push(title); });
 
if (numberOfSamples >= allTitles.length) { return allTitles; }
 
for (var i = 0; i < numberOfSamples; i++) {
    sampleIndex
= Math.floor(Math.random() * allTitles.length);
    samples
= samples.concat(allTitles.splice(sampleIndex, 1));
 
}
 
return samples;
};

})();


TonyM

unread,
Sep 9, 2019, 9:56:49 PM9/9/19
to TiddlyWiki
Devin,

Thanks for sharing. This would be very useful to some. Could you please outline how to install it for use so a broader audience can make use of it.?

Regards
Tony

Mohammad Rahmani

unread,
Sep 10, 2019, 2:51:56 AM9/10/19
to tiddl...@googlegroups.com
Great share Devin!

It would be nice to add a small instruction for people how like to use your script / filter in their wiki!

Best wishes
Mohammad


--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/7ad1fb46-382e-417c-a5a4-6e412274a2e8%40googlegroups.com.

Mohammad Rahmani

unread,
Sep 10, 2019, 1:11:51 PM9/10/19
to tiddl...@googlegroups.com
One question, 
How about the performance effects? It seems when a tiddler uses this filter every change on the screen trigger the simple filter!


Best wishes
Mohammad


On Tue, Sep 10, 2019 at 12:35 AM Devin Weaver <weaver...@gmail.com> wrote:

@TiddlyTweeter

unread,
Sep 10, 2019, 1:21:27 PM9/10/19
to TiddlyWiki
One query. DO we need JavaScript for that?

I thought it was doable in TW already?

TT


On Tuesday, 10 September 2019 19:11:51 UTC+2, Mohammad wrote:
One question, 
How about the performance effects? It seems when a tiddler uses this filter every change on the screen trigger the simple filter!


Best wishes
Mohammad


To unsubscribe from this group and stop receiving emails from it, send an email to tiddl...@googlegroups.com.

Mohammad Rahmani

unread,
Sep 10, 2019, 1:41:37 PM9/10/19
to tiddl...@googlegroups.com
Hi TT
Is there any filter or math operator to do this?


Best wishes
Mohammad


To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/21677588-663c-40ad-8292-745c5a2f0b18%40googlegroups.com.

@TiddlyTweeter

unread,
Sep 10, 2019, 1:53:58 PM9/10/19
to TiddlyWiki

Mark S.

unread,
Sep 10, 2019, 2:31:58 PM9/10/19
to TiddlyWiki
That's all based on Tobias' random filter. Looking at the code, it appears nearly identical Devin's. I suppose there's only so many
ways to wrap that functionality.

I guess one slight improvement would be to pass in context that would prevent the number from changing every time the tiddler was refreshed.
Perhaps use the suffix (as if a field) to indicate "this usage here.". So it would be locked in until the browser reloaded.

Note that all random implementations are pseudo random. Supposedly browsers now have an api, window.crypto.getRandomValues, that
produces better random values. I don't know how they can do that -- it's like pulling a rabbit out of thin air. The randomness thing only
matters if you're doing things like encrypting critical data, pulling numbers for a lottery or doing a research study.

@TiddlyTweeter

unread,
Sep 10, 2019, 2:55:42 PM9/10/19
to TiddlyWiki
Ciao Mark et al

I wish we had better ways to improve the wheel other than someones serendipitous memory of times past. 

Mark makes interesting points about the refresh process and the underlying api. 

TT

Mohammad Rahmani

unread,
Sep 11, 2019, 3:38:55 AM9/11/19
to tiddl...@googlegroups.com
Which of those solution does not use JS?

Of course I think in 5.1.20+ it is possible to use math operators plus the now macro to create random number


Best wishes
Mohammad


To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/1678c4fe-eb83-4ec7-8b88-087393253082%40googlegroups.com.

Mohammad

unread,
Sep 11, 2019, 10:05:20 AM9/11/19
to TiddlyWiki
Thanks Mark!
 Yes, in TW actually this is useful when one displays a random tiddler content for example at startup!
I have post a question in separate post on this.

--Mohammad
Reply all
Reply to author
Forward
0 new messages