Can I generate a random number within a range?

I have a need to pick a ‘sudo’ random number from 1-10 in my Tera templates. I’ve looked at a bunch of things and cannot find anything obvious to help me so I thought I would ask here.

Anyone come across a solution for this, I did think about using now() | date(format="%.2f") and format date to get nano seconds and try something with that. But before I set off on that I thought I would ask here for an approach.

EDIT: Ok a short time later…

tried this:

{% for p in range(end=10) %}
number: {{ now() | date(format="%2f") }} <br>
{% endfor %}

And I get thread panics at a display implementation Error 5 and a whole bunch of stuff? if this not safe?

Thanks

Looks like a bug!

OK thanks for looking at it.

It looks like a bug in chrono itself. %2f doesn’t exist, there is 3f, 6f, 9f and it just panics at runtime when trying to format an unknown format apparently?

Found it: https://github.com/chronotope/chrono/issues/47
It’s now fixed in Tera and will be in the next Zola version.

1 Like

OK thanks not much I can do about that. I will think about it.

I raised this on Tera git as a requested enhancement. Seemed to be more appropriate. I could not find a sensible way of doing this without having something like the the Jina2 equivalent random(seq) template function.