Created
March 22, 2023 23:15
-
-
Save nicolevanderhoeven/ce9dd00f049996ccf1fe7f97946297d1 to your computer and use it in GitHub Desktop.
Randomly return 10 NPCs from an Obsidian vault using the Dataview plugin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```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