Pull Random List of Properties using Dataview

Pull Random List of Properties using Dataview

Hello! I’m trying to learn to use random generators in dataview. Today, I’m trying to pull five random properties I use to organize my notes.

Things I have tried

I am not a coder, so this is cobbled together from notes I’ve found online. This is what I have so far:

WHERE PageType
date(now) as Now 
(file.mtime.year + file.mtime.hour + file.mtime.day + file.mtime.hour + file.mtime.minute + file.mtime.second + file.size + Now.hour + Now.minute + Now.second) * 15485863 as Hash 
((Hash * Hash * Hash) % 2038074743) / 2038074743 as Rand  
WHERE max(Rand)  
GROUP BY PageType 
SORT Rand  

This is what it looks like when it runs:

This is just what I want, except the content of the list doesn’t randomize from the pool of about 20 potential options. When I run this same code using file names, it randomizes fine.

If anyone has thoughts on why this isn’t working, or thoughts on how to better understand randomization in Obsidian in general, I would very much appreciate it!

Here is some untested code which should return five random page types each day.

WHERE PageType
FLATTEN PageType as pt
SORT hash(dateformat(date(today), "YYYY-MM-DD"), pt)

This code relies on a hash code generator which here takes two elements:

  • The actual page type which is unique within your list
  • A formatted date which has a fixed value any given date

The combination gives a unique fixed value only changing when the date changes, and when hashed this gives a seemingly random return each day.

Your solution using the file.mtime and now isn’t as reliable. First of all each time you execute your query the now will stay at a fixed value, and if some of your files provide multiple of the page types they’ll also contribute the same into your hash function.

So without doing the math I’m guesstimating your to reliant on your formula (which given small changes in the file modification times between runs) always gives the same sequence of numbers which you don’t really change the order of by your formula will mostly give the same random sequence.

Using the hash() I provided it’ll produce vastly different result if only one character changes.

Thank you! Is there a way to make it so this can be refreshed more often than every day? I typically use this sort of function multiple times in an afternoon.

The thing about refreshing queries is usually to keep getting a persistent result if/when the query/page refreshes. This happens all the time, and sometimes twice on the switching of editor modes.

With that bein said you can change the party to any time interval you’d like to give you new lists. Add in the current hour using “YYYY-MM-DD-hh” and you’ll get new lists each hour. Add in the minutes part, and you’ll get new list each minute (and divide minutes by 15 for each quarter)… In other words you can control the time interval using different date formats.

Or if you like that you could get a given random list for a given file by using file.name instead of the date variant.

Thank you for the added info! (And sorry for the slow back and forth on this. I’ve been moving these last two weeks, which has made it difficult to check on this).

I tried to give the code above a shot and I got this result:

Do you have any idea how to fix what broke? Again, sorry for the novice experience level. I have no background in coding at all, so every part of this is new to me.

It seems like you might be running on n an older version of Dataview, so you might want to update your community plugins.

There we go! That works! Thank you so much for your help. One very last question, if you’re willing: How would you apply the same randomize method to something other than a list of properties?

I tried to apply the code to a search by page name and Obsidian wouldn’t have it.