random macro, random filter

215 views
Skip to first unread message

Mohammad Rahmani

unread,
Sep 11, 2019, 3:47:38 AM9/11/19
to tiddl...@googlegroups.com
As there is no random filter (I expect to have it with math operators) in TW core, is it possible using machine clock and TW 5.1.20+ math ops create a macro to generate random numbers?


Best wishes
Mohammad

PMario

unread,
Sep 11, 2019, 5:56:32 AM9/11/19
to TiddlyWiki
Hi,

It depends on what you want to do with it. math.random() isn't really random, but it should be good enough to show a random tiddler at startup, or for RPG games.

Secure random numbers are also possible with latest browser Web-crypto API.

-m

Jeremy Ruston

unread,
Sep 11, 2019, 6:10:55 AM9/11/19
to tiddl...@googlegroups.com
Hi Mohammad

On 11 Sep 2019, at 08:47, Mohammad Rahmani <mohammad...@gmail.com> wrote:

As there is no random filter (I expect to have it with math operators) in TW core, is it possible using machine clock and TW 5.1.20+ math ops create a macro to generate random numbers?

This has come up before, it might be helpful to dig up the old references.

The reason there is not a simple [random[]] operator is that filter operators are supposed to be deterministic: they must return the same results for the same inputs, regardless of when they are called. This property is required for the refresh mechanism to operate correctly. In particular, using a non-deterministic filter with the list widget will cause thrashing on every refresh cycle.

To make it work, one needs to add a seed operand, and initialise that value via a new action widget that can assign a random value to a tiddler. I think somebody made a plugin a few years ago to do this.

Best wishes

Jeremy




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/CAAV1gMA6b-EtuP4KjSWfir9jy2eAh65c-QwnvTL9kQH43qYNCw%40mail.gmail.com.

PMario

unread,
Sep 11, 2019, 7:36:45 AM9/11/19
to TiddlyWiki
Hi,

So what we need for the list-widget is "predictable unpredictability" ... A very nice feature ;)

I think first of all we need some parameters similar, but not the same, as for the range operator.

Additionally I can think about some named "random value" pools, that have different refresh rates. ... eg: $:/state/seed/startup, $:/state/seed/home, $:/state/seed/click ... and so on.

The $:/DefaultTiddlers could look like

HelloThere
[tag[show-me-at-startup]prng:startup[3]]

Which would show HelloThere tiddler and 3 tiddlers tagged "show-me-at-startup"

The state tiddler $:/state/seed/startup will only be seeded at the TW startup.


The action-widget <$action-seed $seed=home /> will create a new home seed. So the "home" button will create a new one.

<$action-seed $seed=xyz /> can be used with: prng:xyz[params]

<$action-seed $seed=abc $prefix=$:/test/seed /> will create a new seed tiddler $:/test/seed/abc

The filter "[prng:test,abc[params]]" can use this value.

I'm not sure, if this proposal is consistent, but I think it could be usable.

have fun!
mario


PMario

unread,
Sep 11, 2019, 7:44:35 AM9/11/19
to TiddlyWiki
One more addition:

$:/state/seed/   .... will not be saved and _not_ be synced ... $:/core/save/all ... needs to be adjusted

$:/temp/seed/ ... will be saved but will _not_ be synced ... $:/config/SyncFilter knows that already


$:/xxx/seed/ ... will be saved _and_ will be synced  ... TW default behaviour atm.

-m

Mohammad Rahmani

unread,
Sep 11, 2019, 8:37:46 AM9/11/19
to tiddl...@googlegroups.com
Hi Jeremy!
I understood why random filter could affect the performance!

Thank you for clarification!



Best wishes
Mohammad


Mohammad Rahmani

unread,
Sep 11, 2019, 9:50:10 AM9/11/19
to tiddl...@googlegroups.com
Mario,
 Many thanks for your explanation!

I do not need an actual random generator! I just need when wiki is opened (startup time) or when a special tiddler is displayed on the screen the content of a random tiddler say tagged with myTag is shown!

This is what already has implemented using JS and random function! But as Jeremy explained and Tobias also noted in his plugin readme both has performance effect!


In summary I need to show a random tiddler say tagged solution at wiki startup nothing else!



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.

Matthew Lauber

unread,
Sep 11, 2019, 11:58:48 AM9/11/19
to TiddlyWiki
I have a random[] filter available here:  https://mklauber.github.io/tw5-plugins

Matthew Lauber

unread,
Sep 11, 2019, 12:02:59 PM9/11/19
to TiddlyWiki
Indeed, it's here:  https://mklauber.github.io/tw5-plugins  At the time, I was running into an issue where I couldn't use <<time "[UTC]YYYY0MM0DD0hh0mm0ssXXX">> as a param to a filter, so I couldn't make it deterministic and easily based on render time.  I'm seeing some interesting ideas here though in Mario's post to create some default seed tiddlers to update.  If there's interest in trying to get this into the core, I'd be happy to work on it.  As it is, my plugin, non-deterministic as it is, hasn't been an issue in my years of using it.

Matt 


On Wednesday, September 11, 2019 at 6:10:55 AM UTC-4, Jeremy Ruston wrote:
Hi Mohammad

On 11 Sep 2019, at 08:47, Mohammad Rahmani <mohamma...@gmail.com> wrote:

As there is no random filter (I expect to have it with math operators) in TW core, is it possible using machine clock and TW 5.1.20+ math ops create a macro to generate random numbers?

This has come up before, it might be helpful to dig up the old references.

The reason there is not a simple [random[]] operator is that filter operators are supposed to be deterministic: they must return the same results for the same inputs, regardless of when they are called. This property is required for the refresh mechanism to operate correctly. In particular, using a non-deterministic filter with the list widget will cause thrashing on every refresh cycle.

To make it work, one needs to add a seed operand, and initialise that value via a new action widget that can assign a random value to a tiddler. I think somebody made a plugin a few years ago to do this.

Best wishes

Jeremy




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 tiddl...@googlegroups.com.

Mohammad Rahmani

unread,
Sep 11, 2019, 12:35:51 PM9/11/19
to tiddl...@googlegroups.com
Matt,

 Thank you for sharing! It uses JS and non-deterministic! So, it should have the issue Jeremy noted!
But based on your previous post, Jeremy and Mario note, also Mark explanation in another post (https://groups.google.com/d/msg/tiddlywiki/jlBObreG30w/moxP900xBAAJ)

Is it possible to make it deterministic to address the performance issue? I mean generate a random number and freeze until
a browser refresh?


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/b401dd79-e59a-4741-ac0e-3e256646d6de%40googlegroups.com.

Matthew Lauber

unread,
Sep 11, 2019, 12:39:42 PM9/11/19
to tiddl...@googlegroups.com
Absolutely.  I think we'd just need something that runs once at load, (which I'd need to figure out how to do, then you could do something like [all[tiddlers]shuffle{$:/temp/seed!!startup}] and it'd be deterministic.  As it is, I usually, use a state field on the tiddler I'm randomizing, and a button to update that field to <<now [UTC]...>>.  That's sufficient that it randomizes well every button press.  

Mohammad

unread,
Sep 11, 2019, 12:45:24 PM9/11/19
to TiddlyWiki
Yep! That's the point!

I would appreciate if you could do this! As noted by others above at the moment there is no good solution to this and yours can solve this issue!

By the way I tested some case on your  https://mklauber.github.io/tw5-plugins
Comparing to other JS solution this is semi-deterministic why?
because for example in solution by Tobias, when you do any kind of operation the value of random is changed (like closing opening adding tiddlers) your does not do this!

Am I right?

--Mohammad

Mohammad

unread,
Sep 11, 2019, 12:48:18 PM9/11/19
to TiddlyWiki
How this work? Seems it is deterministic?

<$button>
<$action-setfield $tiddler="$:/temp/shuffle/example" $field="state" $value=<<now "0hh:0mm:0ss">>/>
Change Order
</$button>

<$list filter="1 2 3 4 5 +[shuffle{$:/temp/shuffle/example!!state}]">

</$list>

Matthew Lauber

unread,
Sep 11, 2019, 12:51:39 PM9/11/19
to TiddlyWiki
@Mohammad: Mine does not monitor/update due to renders of objects other than it's inputs or body.  It's only executed when the list widget itself is rendered.  So unless the input to the list changes, or the body of the list itself changes, it won't re render. 

@Jeremy, where should I put a core tiddler with the $:/tags/StartupAction in the repo?  It's not a javascript module, just a <$action-setfield widget to set a seed at wiki load.

Matthew Lauber

unread,
Sep 11, 2019, 12:52:55 PM9/11/19
to TiddlyWiki
That is deterministic.  The parameter to the shuffle operator is treated as a seed.  So the shuffle of the list will generate the same output, as long as the contents of $:/temp/shuffle/example doesn't change.  But when it changes, the shuffled order will also change.

Mohammad

unread,
Sep 11, 2019, 1:08:24 PM9/11/19
to TiddlyWiki
Matt, That is great! and I think it solve most part of the issue related to performance!

I hope, Jeremy, Mario, and Mark join this discussion and give their opinion!

--Mohammad

Mohammad Rahmani

unread,
Sep 11, 2019, 2:11:31 PM9/11/19
to tiddl...@googlegroups.com
Matt,

Thanks for the PR #3712 for shuffle filter!



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/f0ab0dda-d31c-4cd0-8863-f6806490cbe8%40googlegroups.com.

Mat

unread,
Sep 11, 2019, 2:20:28 PM9/11/19
to TiddlyWiki
Seems you've solved your needs but I'd think
 
<<now "xxx">>

should probably be enough in many cases. It's more or less a random number between 0 and 999.

If we had a modulo operator I think it should be simple to convert it to a random number in any other range. I tried to extract the last digit but a few quick attempts failed - suggestions anyone?

<:-)

Mohammad Rahmani

unread,
Sep 11, 2019, 3:00:28 PM9/11/19
to tiddl...@googlegroups.com
Mat,
 Do you mean something like this

\define pseudo-rand(beta, alpha:1)
<$set name=seed value=<<now "XXX">> >
<$list filter="[<__beta__>subtract<__alpha__>multiply<seed>divide[999]add<__alpha__>round[]]" variable=prand>
<<prand>>
</$list>
</$set>
\end



<<pseudo-rand beta:10>>

If we linearly map [0, 999] into [alpha:1, beta:n] where alpha is the first tiddler and beta is the number of tiddlers to be used for random display!
from mathematics we have

x in [0, 999] (this will be generated by now macro)
y in [alpha, beta] (this is the number we like)

then y= (beta - alpha) / 999  + alpha

In our case if assume number of tiddlers tagged say with myTag is n like 100 then

<<pseudo-rand beta:100>>

n can be calculated values for example from count filter operator.




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/9a97a046-3e7a-4718-afea-8c4a7a8a697a%40googlegroups.com.

Mohammad Rahmani

unread,
Sep 11, 2019, 3:02:05 PM9/11/19
to tiddl...@googlegroups.com
This is deterministic but IS NOT a real number! albeit it works for many purposes like display a random quote at TW startup
or display a random content when a tiddler is shown!


Best wishes
Mohammad

Matthew Lauber

unread,
Sep 11, 2019, 3:26:31 PM9/11/19
to TiddlyWiki
All, I've updated my PR (https://github.com/Jermolene/TiddlyWiki5/pull/3712) to be always deterministic, using first the operand (so you can trigger updates with buttons) next a seed set to the time at startup by startup tiddler, falling back to a static/hardcoded seed.

@Jeremy, there's a question on that PR for you regarding where you want that startup tiddler to belong in the core.  

Here's hoping we can get this over the finishline for 5.1.22!

Matt


On Wednesday, September 11, 2019 at 3:47:38 AM UTC-4, Mohammad wrote:

Mohammad

unread,
Sep 11, 2019, 3:39:15 PM9/11/19
to TiddlyWiki
Thanks Matt!

TonyM

unread,
Sep 11, 2019, 8:22:01 PM9/11/19
to TiddlyWiki
Folks,

I have come late to this conversation, presumably because I live in a different timezone, so please excuse me if it was addressed in the details.

A Couple of points

  • The use of appropriate seeds is a well known way of making something effectively random. Surely once we have it the last saved time + the current time would be effective seeds. this would initiate a different pattern every load, and every time the random function is used.
  • For a one shot random items this may not be relevant but if one really wants to shuffle a list the whole list should be shuffled and then deterministically selected, or each time something is randomly selected remove it from the list of potential items otherwise a pure random process will return to the same item before all items have being see on average 50% of the time.  
Regards
Tony

Mat

unread,
Sep 12, 2019, 12:42:06 AM9/12/19
to TiddlyWiki
Mat wrote:
<<now "xxx">>

should probably be enough in many cases. It's more or less a random number between 0 and 999.

Error: should be capital X's:

<<now XXX>>

<:-)

Mohammad

unread,
Sep 12, 2019, 2:47:11 AM9/12/19
to TiddlyWiki
This solution seems that is not deterministic! When you use now macro in list and set widget to generate  a pseudo random number for displaying a random tiddler!
See my macro above using this technique!

--Mohammad

PMario

unread,
Sep 12, 2019, 4:39:25 AM9/12/19
to TiddlyWiki
On Wednesday, September 11, 2019 at 9:26:31 PM UTC+2, Matthew Lauber wrote:
All, I've updated my PR (https://github.com/Jermolene/TiddlyWiki5/pull/3712) to be always deterministic, using first the operand (so you can trigger updates with buttons) next a seed set to the time at startup by startup tiddler, falling back to a static/hardcoded seed.

@Jeremy, there's a question on that PR for you regarding where you want that startup tiddler to belong in the core.  

Here's hoping we can get this over the finishline for 5.1.22!

I think, we should talk a little bit more about the specification and then about the implementation. 

-m

@TiddlyTweeter

unread,
Sep 12, 2019, 7:13:32 AM9/12/19
to TiddlyWiki
PMario wrote:
I think, we should talk a little bit more about the specification and then about the implementation. 

I can't help technically.

But I can comment on how I could use it...

1 - To select a number between 0000 and 3999 (use for the random showing of an external image library where images are named 0000fw.jpg through to 3999fw.jpg) . 
 Click and next random loads. PS: There are no tiddlers for the images, its simply invocation of an external filename in a gallery.

2 - Rotation. To provide fresh content on start-up randomly loading 1 of 30 different tiddlers tagged "startup-30". They may have any title.

Best wishes
TT

PMario

unread,
Sep 12, 2019, 7:48:17 AM9/12/19
to TiddlyWiki
On Thursday, September 12, 2019 at 1:13:32 PM UTC+2, @TiddlyTweeter wrote:
PMario wrote:
I think, we should talk a little bit more about the specification and then about the implementation. 

I can't help technically.

But I can comment on how I could use it...

Yes, that's what is needed atm.
 
1 - To select a number between 0000 and 3999 (use for the random showing of an external image library where images are named 0000fw.jpg through to 3999fw.jpg) . 
 Click and next random loads. PS: There are no tiddlers for the images, its simply invocation of an external filename in a gallery.

This points out, that in your usecase you may have up to 4000 images. ... The "number space" with 4 digits is 10000 images 0000 ... 9999.

So "auto" creating names with eg: the range operator isn't possible. Because it doesn't know about a "leading zeros" template like the TW date-format.

IMO "edge-case" info like this is important, prior to an implementation.
 
2 - Rotation. To provide fresh content on start-up randomly loading 1 of 30 different tiddlers tagged "startup-30". They may have any title.

This seems to be a standard-case szenario.

-m

PMario

unread,
Sep 12, 2019, 8:01:30 AM9/12/19
to tiddl...@googlegroups.com
On Thursday, September 12, 2019 at 1:13:32 PM UTC+2, @TiddlyTweeter wrote:
PMario wrote:
I think, we should talk a little bit more about the specification and then about the implementation. 

I can't help technically.
But I can comment on how I could use it...

My usecase is a little bit different. ... I want to have "replay-ability".

Eg:
 - I want to programmatically create several 10000s of tiddlers with some text, tags, fields, aliases ...
    - I want to use wikitext for this .. if possible
    - I'm sure I can tiddlywiki --build it on the command line
 - Then I want to open a list eg: [tag[tag001]] where 001 is a random number but the tag exists
 - I want to measure the time, that is needed.
   - Not JS time, but UX time

That's cool for me. I can publish the measurements. ... BUT nobody can validate my measurements, because it's hard to reliably "replay" it.

So I'd like to have predictable random number generators here. ... Several of them.

If I can give you the "seed-values" or even better: 1 seed value, so you can reliably replay it.

have fun!
mario

Matthew Lauber

unread,
Sep 12, 2019, 8:13:10 AM9/12/19
to tiddl...@googlegroups.com
So, I can't help with the random number generator, my shuffle filter isn't designed to handle that.  It just takes an input list, randomizes the order of it, and returns the new list.  But you can have several deterministic orders Mario, just pass in different seed values as the parameter.  The only thing being randomized is the "no param" seed value, and that'll be stable throughout the time a wiki is open. 

Edit: But re: leading zeroes, it occurs to me that that is a problem better addressed in the range operator.  Because then shuffle could still work on it, and you could also simply generate the list and iterate it to get all the values.  

PMario

unread,
Sep 12, 2019, 8:33:07 AM9/12/19
to TiddlyWiki
On Thursday, September 12, 2019 at 2:13:10 PM UTC+2, Matthew Lauber wrote:
So, I can't help with the random number generator, my shuffle filter isn't designed to handle that. 

That's OK. ... but wouldn't it be nice if the core would provide some untility-random API, that you could use, instead of importing one?

I had a very short look about the code of the seedrandom() function, and there are some implementation details, which I'm not happy with.

More info can be seen at the github issue.

-m

TonyM

unread,
Sep 12, 2019, 9:01:53 PM9/12/19
to TiddlyWiki
Folks

Leading zeros using range are possible. Increment a number like 0.0001 by 0.0001 and remove the prefix "0."

hours would be 0.01

Regards
Toby

PMario

unread,
Sep 13, 2019, 3:13:56 AM9/13/19
to TiddlyWiki
hmmm,

That's right, but shouldn't it be a KISS.

-m

TonyM

unread,
Sep 13, 2019, 7:42:21 AM9/13/19
to TiddlyWiki
I agree Mario but a few discussions suggest it would be quite complex to make it simple, especially to maintain leading zeros in the various places numbers are used.

Perhaps a custom operator would help.

Regards
Tony

Reply all
Reply to author
Forward
0 new messages