Skip to content

Instantly share code, notes, and snippets.

@nicolevanderhoeven
Created March 22, 2023 23:15
Show Gist options
  • Save nicolevanderhoeven/ce9dd00f049996ccf1fe7f97946297d1 to your computer and use it in GitHub Desktop.
Save nicolevanderhoeven/ce9dd00f049996ccf1fe7f97946297d1 to your computer and use it in GitHub Desktop.
Randomly return 10 NPCs from an Obsidian vault using the Dataview plugin.
```dataviewjs
function randomElements(arr, n) {
var result = new Array(n);
var len = arr.length;
if (n > len) throw new RangeError("randomElements: more elements taken than available");
for (var i = len - 1; i >= len - n; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = arr[i];
result[len - i - 1] = arr[j];
arr[i] = arr[j];
arr[j] = temp;
}
return result;
}
dv.list(randomElements(dv.pages('"TTRPGs/Temporary White Circle"')
.where(b => b.type == "NPC")
.where(b => !b.deceased)
.sort(b => b.file.link)
.map(b => b.file.link),10)
);
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment