Jump to a Random Page Using a Static Site Generator

To implement a ‘jump to random page’ in a static site (like this website) can be done using a template that loads a JavaScript function to redirect the browser. Static site generators typically have a way of iterating over all pages. You can use this same functionality to generate the random page function.

For example, using hugo:

<!--
     This is a way of redirecting the user to a random note. This requires
     a full index of all notes and so it's on its own page. To take a user
     to a random note, simply link to this page.
-->
<script>
 const pages = [{{ range .Site.AllPages }}{{ if not .IsHome }}"{{ .Permalink }}",{{ end }}
   {{ end }}
 ];
 let random_idx = Math.floor(Math.random() * (pages.length - 1));
 window.location = pages[random_idx];
</script>
{{ end }}

See also: