Pull one random quote (using custom checkbox) into note

What I’m trying to do

I’d like to have a random quote displayed on my dashboard page. All my quotes are logged using a custom checkbox ["], so I haven’t been able to find an existing code that utilizes these. Not picky if this changes by day or on page reload.

I’m told this would likely be a dataviewjs query, but I don’t know how to write these.

Things I have tried

I’ve tried a couple different methods using standard dv queries, but I’m only able to really figure out how to pull all of them. It’s specifically the ‘random’ part I’m struggling with.

I’ve tried digging through the dataview documentation, but I don’t have enough of a foundation to really understand the js portions.

I briefly asked about this in Discord, which is how I found out it needed dataviewjs, but it seemed like a bigger ask than appropriate there.

Various variants of this question has arisen over the last few months, the one below is my take which might be the closest to your requirements:

This should be possible to modify to a query that lists tasks with your custom status, and display a random one of them.

The question however, is more related to how you would like to trigger this. Is your dashboard kind of static in the sense, that it’s not easy to trigger a template into it (like the above code is utilising)?

Doing random stuff with just dataview tends to be a little to random, as each time you elsewise change the dashboard file (or reload it), the quote would change. Would that be something you’d still would like to use?

Would it be an option to have a button to generate a new quote? (I’m not quite sure how to do that, especially since I’m not using a dashboard myself, and I’m not sure how you format the part of the dashboard that should show the quote)

How to modify a query to return something random

Lastly, one trick to make a query return something random is to sort it according to something more or less random, like modification time in milliseconds divided by some number and then just use the first entry from that list. It’s not guaranteed to be a uniform random distribution, but it does produce some random output.

It’ll collapse a little if your quotes are all gathered in a few files, as it then tend to choose the first in that file. This could possible be countered by assigning numbers to the item in some random fashion.

So before presenting any code; Are most of your quotes gathered in a few files? Would you suffice with a query which changes the quote whenever Dataview chooses to rerun the query?

Given they’re scattered all around, and you’re happy with them changing “all the time”, you could try something like the following:

FLATTEN file.tasks as item
WHERE item.status = "\""
SORT (number(dateformat(file.mday, "x")) + number(dateformat(date(now), "x")) ) % 10111

Another variant over the same theme could be:

FLATTEN file.tasks as item
WHERE item.status = "\""
SORT (item.position.start.line * item.position.start.offset + number(dateformat(date(now), "S")) ) % 53

This’ll use the start line and offset plus the millisecond part of current time to calculate a number which do the modulo 53 on. This variant has the advantage that it’ll randomise stuff within the same file.

There are stuff to be said on choosing the “correct” prime number to use, which I’m not going to go into now, but if I remember correctly you need to use a number which is greater than the number of quotes you’ve got…

I’ve been trying to do this lately, but talking about triggers: I only want it to be on note creation. Essentially i want a random quote (from a file of quotes ) added to each day; been trying to do it in Templater, but I haven’t managed it thus far - the dice roller plugin does it, but it changes every time the file is opened, not just at creation. Any thoughts here on that?

See the link above in my first reply. It offers up to 5 random notes, and they’ll stay there and not change.

Change the query to list your notes, and have just 1 instead of 5, and you should be set.

1 Like

Ooh, reading fail on my part! Saw your later posts in the thread but somehow missed the first one. That’s perfect, I can adapt that to exactly what I’m after pretty easily. Thanks! :pray:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.