A button to open a random tiddler

202 views
Skip to first unread message

passingby

unread,
Feb 19, 2018, 9:10:23 PM2/19/18
to TiddlyWiki
Can anybody suggest a way to have a button which opens a random tiddler out of all the tiddlers tagged with a particular tag?

Matthew Lauber

unread,
Feb 19, 2018, 9:46:48 PM2/19/18
to TiddlyWiki
I've written a shuffle operator that can randomize the order of a list.  It's available here:  https://mklauber.github.io/tw5-plugins/

To use it in that way, you'd probably want something along the lines of 

<$button>
<$list filter="[tag[Tag]shuffle[]first[]]">
<$action-navigate $to=<<currentTiddler>> />
</$list>
Open
</$button>

Diego Mesa

unread,
Feb 19, 2018, 9:52:50 PM2/19/18
to TiddlyWiki
Hey Mathew,

How can you install the shuffle operator?

Diego

Matthew Lauber

unread,
Feb 19, 2018, 10:16:34 PM2/19/18
to TiddlyWiki
  1. Go to https://mklauber.github.io/tw5-plugins/ in one window, open your wiki in another.  
  2. Drag the link "Matt's Plugin Library" to your wiki.  
  3. Then click the Cog to get to settings, and go to the plugins tab.  Click "Get More Plugins"
  4. Click "Open Plugin Library" below "Matt's Plugin Library"
  5. look for the plugin with "Shuffle filter operator" in the description and click install.  
  6. Reload your wiki.  It should work now.

passingby

unread,
Feb 20, 2018, 12:55:03 AM2/20/18
to TiddlyWiki
Thank you Matthew. This would work well.

passingby

unread,
Feb 20, 2018, 3:00:13 AM2/20/18
to TiddlyWiki
Matthew
I tried the plugin. Its a wonderful code in that it is not a macro but a filter operator, which is very interesting!

And it does work as intended. But i am seeing that it repeats one or two tiddler a lot. At first i thought maybe my tiddlers were not enough in numbers but then i have around 25 tiddlers and it still repeats a couple initial tiddlers it picks up, a lot.

Could you suggest anything for this?

Thank you for your code.

Stephan Hradek

unread,
Feb 20, 2018, 4:00:10 AM2/20/18
to TiddlyWiki
I think there is a big flaw in Matthew's code.

Instead of using a real random function there is this code:

Math.floor(Math.abs(Math.sin(seed++)) * currentIndex);

There are 2 flaws here I think:
1. sin isn't very "random"
2. sin goes from 0 to 1. This will lead to a just shuffled element being shuffled a second time. Even worse it can lead to array index out of bounds if the seed is (n * pi)+(pi/2). Try it with seed= 3.14159265359/2 ;)

will open an issue on github

Stephan Hradek

unread,
Feb 20, 2018, 5:03:42 AM2/20/18
to tiddl...@googlegroups.com
This was wrong: "Update: Upon inspecting the code a bit more: seed cannot be a multiple of pi + pi/2 as it's always a 32 bit integer."

shuffle indeed has an issue. besides that I think randomness of |sin| isn't the best.

Try this tiddler:

<ol>
<$list filter="a b c d e f +[shuffle[\x04\x09\x03\x08\x03\x0e]]">
<li><<currentTiddler>></li>
</$list>
</ol>

output will be

  1. a
  2. d
  3. e
  4. b
  5. c

  6. f

This is due to the fact that my random seed-generating-string is carefully crafted so that |sin| will be 1. That way the first shuffle is from array[6] (out of bounds) with array[5] (last index)

There are 28 seeds where this happens:
  1. -2015008331
  2. -1849301266
  3. -1769157409
  4. -1603450344
  5. -1437743279
  6. -1357599422
  7. -1191892357
  8. -946041435
  9. -780334370
  10. -700190513
  11. -534483448
  12. -368776383
  13. -288632526
  14. -122925461
  15. 122925461
  16. 288632526
  17. 368776383
  18. 534483448
  19. 700190513
  20. 780334370
  21. 946041435
  22. 1191892357
  23. 1357599422
  24. 1437743279
  25. 1603450344
  26. 1769157409
  27. 1849301266
  28. 2015008331

Stephan Hradek

unread,
Feb 20, 2018, 6:02:36 AM2/20/18
to TiddlyWiki

Stephan Hradek

unread,
Feb 20, 2018, 7:25:30 AM2/20/18
to TiddlyWiki


Am Dienstag, 20. Februar 2018 09:00:13 UTC+1 schrieb passingby:
But i am seeing that it repeats one or two tiddler a lot. At first i thought maybe my tiddlers were not enough in numbers but then i have around 25 tiddlers and it still repeats a couple initial tiddlers it picks up, a lot.

Matthew Lauber

unread,
Feb 20, 2018, 7:37:54 AM2/20/18
to TiddlyWiki
Dang!  I go to sleep for 8 hour and your guys pick apart my code.  Seriously though, thanks guys.  I'll take a look at improving the randomness of my shuffle operator.  I seem to recall not using the javascript math.random function because I wanted to be able to seed the function and get a list ordering that only changed when I clicked a button.  I'll see what I can find for that.  

Stephan Hradek

unread,
Feb 20, 2018, 7:40:02 AM2/20/18
to TiddlyWiki


Am Dienstag, 20. Februar 2018 13:37:54 UTC+1 schrieb Matthew Lauber:
Dang!  I go to sleep for 8 hour and your guys pick apart my code.  Seriously though, thanks guys.  I'll take a look at improving the randomness of my shuffle operator.  I seem to recall not using the javascript math.random function because I wanted to be able to seed the function and get a list ordering that only changed when I clicked a button.  I'll see what I can find for that. 

Matthew Lauber

unread,
Feb 20, 2018, 8:49:17 AM2/20/18
to TiddlyWiki
I've reworked the shuffle operator to use MAth.random and to use https://github.com/davidbau/seedrandom/ to seed Math.random.  So, the issues mentioned earlier should be resolved.

passingby

unread,
Feb 20, 2018, 11:43:51 AM2/20/18
to TiddlyWiki
Guys! Stephan, Mathew Thank you for your efforts. I am always impressed with all that mathematics! Although I did take up math in my high school, i barely passed in it. I remember sine cos theta angles but nothing more than names. But there is always a lurking desire to spend some time on improving my math skills and knowledge. I secretly think this is going to make me smarter. :-)

But thanks again, I shall try out the changes.
Reply all
Reply to author
Forward
0 new messages