So I’m in kind of a strange mood, and had an idea, which I went on to implement. The idea was that if you gather all the tasks, and generate a hash based upon the task text, it should be possible to get a random set of tasks. And if you then continue to select a set based on a slow changing variable, you would get the same random set, as long as the set chooser is the same.
One day to get such a set chooser could be to use the day of year modula some number
, which would return the same set every number
of days.
Actual code
The code is somewhat long, but it does also include some debug stuff like the display of the full task list with hash codes and set numbers, and so on. Hopefully it’s understandable where and what to change to make it actual useful code.
```dataviewjs
// Alternative 1: A proper hash generator
function hashCodeProper(str) {
return str.split('').reduce((prevHash, currVal) =>
(((prevHash << 5) + prevHash) + currVal.charCodeAt(0))|0,0)
}
// Alternative 2: Simple addition of the charcode values
function hashCodeSimple(str) {
return str.split('').reduce((prev, curr) =>
(curr.charCodeAt(0) + prev) |0,0)
}
// How many random set of tasks to get
const randomSetCount = 7
// Select which set of tasks, depending on the day of the year
const taskSet = dv.date("today").day % randomSetCount
// Gather all task candidates
const tasks = dv.pages('"AnotherFolder" or "ForumStuff/f48094"').file.tasks
// Loop through tasks, generate hash codes
// and pick a random assortment of tasks
let randomTasks = []
for (let task of tasks) {
// Change in line BELOW HERE if you want to change hash code function
task.hashCode = hashCodeSimple(task.text + task.path)
task.modHashCode = Math.abs(task.hashCode % randomSetCount)
if (task.modHashCode == taskSet)
randomTasks.push(task)
}
const sortedTasks = tasks.sort(t => t.modHashCode)
// Show all tasks, sorted on the modula hash code
dv.header(2, "All tasks")
dv.table(["Mod " + randomSetCount, "Hash", "Task"],
tasks.sort(t => t.modHashCode)
.map(t => [t.modHashCode, t.hashCode, t.text]))
// Show the random list of tasks
dv.header(2, "Random tasks")
dv.taskList(randomTasks, false)
```
I’ve provided two different hash code generators, if you want to see the other one, change the names of hash code at the indicated spot in the code. You do also of course need to change the selector of tasks to something appropriate for your settings.
In the code, shown above, I currently set the number of sets to 7, which would indicate that the set of random tasks repeated every 7 days. This number should be changed accordingly to how many of tasks you’ve got, and how many random task you would want in each set. Given a larger set of tasks, maybe something like 23 would be nice, as it then would take 23 days before getting the same task set, and that would be on a different weekday.
Sadly, depending on the hash code generation, the distribution of tasks could be skewed so that you get more or less task in each set. This would however be reduced as the task number grows.
This approach would preserve the randomness with regards to adding tasks, as the hash code (and thusly selection of task) is a constant as long as the task text doesn’t change.
Sorry, if this way of dealing with getting a random task is way above your comfort level, or outside your expected solution, but I do believe it addresses some of the remaining issues, namely:
- Using the hash based upon the task text gives a predictability on the randomness
- Using the day of year (modula some number) would keep the randomness to only change every day
Finally, the code also lends itself to variations as doing variations in the loop could easily create various other options. Like you could drop the entire hash code generation, and just select tasks based on some other qualities you see fit.