Load a random file

I have six files quiz1.yml, quiz2.yml etc and want to load their data randomly with a function like:

{{ with .Site.Data.quiz.Math.floor(Math.random() * 6);) }}

I tried index function as well, but can’t get it to work. Maybe someone can help me?
Thank you!

All untested, but shuffle is your friend. And then, don’t forget, it’s only random while creating the website, not when loading the actual page. That is out of the scope of Hugos wizardry and needs some javascript (with the same approaches as below, just in JS):

Version 1:

{{ $item := seq 6 | shuffle | first 1 }}
{{ $thefile := getPage (printf "filename-%1.md" $item }}

Not sure if it starts with 0 or 1.

Version 2:

{{ $filename := shuffle (slice "file1.md" "file-other.md" "some-other-file.md") | first 1 }}

Just so you learn today, that you can’t just put a dot between things here is a snippet that works with your attempt: If Math.floor(Math.random() * 6) gives you a random integer from 1 to 6 then this should work:

{{ $theIndex := printf "quiz%s" Math.floor(Math.random() * 6)) }}
{{ with (index .Site.Data $theIndex) }}

But I somehow think my solutions above are better. I have no supporting evidence though :smiley:

Dear Patrick,
thank you for assistance.

shuffle indeed looks usable here, but so far I don’t see how to specifically use it.

I tried Math option:

{{ $theIndex := printf "quiz%s" (Math.floor(Math.random() * 6)) }}
{{ with (index .Site.Data $theIndex) }}

- it throws an error message: function “Math” not defined

In .Site.Data I have 6 files quiz1.yml, quiz2.yml … quiz6.yml. I want to load their content using a shuffle or math construct like this:

{{ $theIndex := printf "quiz%s" (Math.floor(Math.random() * 6)) }}
{{ with (index .Site.Data $theIndex) }}


{{ end }}