Random operator?

150 views
Skip to first unread message

Hubert

unread,
Apr 23, 2020, 7:52:20 AM4/23/20
to TiddlyWiki
Hello,

It appears that Mathematics Operators don't currently include a random operator.

I know this feature can be easily written as a macro with just a few lines of js or, otherwise, the fomula plugin already ships with random out of the box.

However, I think it would be a great to have the random operator in the core, just like the range operator was added not so long ago.

Just some thoughts.

Regards,
Hubert



TonyM

unread,
Apr 23, 2020, 8:06:16 AM4/23/20
to TiddlyWiki
Hubert,

That could be useful - however I thing some of the maths plugins include this eg evans formula plugin.

Also using now to return the milliseconds may be quite helpful

<<now "0XXX">>

However just for one shot, not in a list.

Regards
Tony

Hubert

unread,
Apr 23, 2020, 8:11:18 AM4/23/20
to TiddlyWiki
Hi Tony,

Also using now to return the milliseconds may be quite helpful

<<now "0XXX">>

Wow, that's very creative! Thank you. Through some further tweaking of that we might even be able to define the min and max integers for the random range as well.

That could be useful - however I thing some of the maths plugins include this eg evans formula plugin.

Yes, Evan's formula has been my go-to maths plugin for years, also for random.

Thanks again,
Hubert

Hubert

unread,
Apr 23, 2020, 8:24:22 AM4/23/20
to TiddlyWiki
OK, I've written this quick and dirty one-liner JavaScript macro that takes two arguments: min and max (ideally integers; any floats are floored down on output). Input arguments (min and max) are included in the random output.

Shorthand syntax below. In this example the macro will output an integer between 10 and 30 (inclusive).

<<Random 10 30>>

File attached if anyone finds it useful.

Regards,
Hubert
$___Macros_Random.js.json

TonyM

unread,
Apr 23, 2020, 10:07:15 AM4/23/20
to TiddlyWiki
Hubert,

Thanks for sharing !

Tony

Jeremy Ruston

unread,
Apr 23, 2020, 10:37:26 AM4/23/20
to tiddl...@googlegroups.com
There’d been some discussions about this before. A straightforward macro like this can cause problems if not used carefully.

There are some subtle assumptions that filter execution is deterministic (ie, a given filter string will evaluate to the same list every time it is executed in the context of the same tiddler store values). For example, the list widget re-evaluates the filter each time it is refreshed, and detects changes to the list. If there were a randomising filter operator then the filter would be deemed to have changed every time, leading to continuous repeated refreshing of the list widget content.

Matt Lauber has contributed a shuffle operator that should be more robust, but has yet to be finished off:


Best wishes

Jeremy



-- 
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/006916c1-7873-4933-b8ae-715f6259e8a0%40googlegroups.com.

Hubert

unread,
Apr 23, 2020, 10:58:52 AM4/23/20
to TiddlyWiki
Thank you Jeremy for pointing this out. I understand the implications and dangers, especially when random is part of a filter run.

My use case is very simple, I only intend to generate an integer, which should be its final output (so, actually a string) and not an argument in any subsequent macro or filter operation.

The reason I originally asked for a random filter operator was based on the assumption that filters are central to so many things within TiddlyWiki, including maths.

I wasn't aware of the proposed shuffle operator. I will follow the developments.

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

Mat

unread,
Apr 23, 2020, 4:48:27 PM4/23/20
to TiddlyWiki
Coincidentally I played with these exact things just the other day. Here's what I made (which I understand it not exactly what you were after, Hubert). It uses the exact technique Tony gave here above and it takes a filter as input and outputs a random filter title:

\define rand(filter)
<$set name=r1 value=<<now 0XXX>>>
<$set name=r2 filter="[<r1>splitregexp:title[]rest[]join[]]" >
<$set name=nth filter="""$filter$ +[count[]multiply<r2>divide[100]ceil[]]""">
{{{ $filter$ +[nth<nth>] }}}
</$set>
</
$set>
</$set>
\end

<:-)

Mohammad

unread,
Apr 23, 2020, 5:39:33 PM4/23/20
to TiddlyWiki
Added to TW-Scripts!
Reply all
Reply to author
Forward
0 new messages