Global Hugo Variables Initialized on Build

I am curious if there’s any way to initialize global variables on every build. For example, here’s a snippet I have:

{{ .Scratch.Add "avatar" (index (shuffle(seq 1 4)) 0) }}

I want this avatar variable to be initialized only once(because of uniformity reasons throughout the website) on every build since it’s supposed to be random. So here’s my question, is it possible to do so? If so, how?

You should look into environment variables and getenv

Compiling from terminal:

env HUGO_RAND=generate_random_number_script hugo

From your template:

{{ getenv "HUGO_RAND" }}
1 Like

The random generator used by shuffle is seeded (by the current time) once every build so it should be stable during a build.

@bep It isn’t stable, I used it to randomly select an image and I got two different images at two different partials.

I’d like to avoid depending on the CLI too much because it would make my theme a bit complex for the others to use.