本站在允许 JavaScript 运行的环境下浏览效果更佳

Random Number Generation and Formatting in Thymeleaf

All examples presented in this article have been tested on Thymeleaf 3.1 and are applicable to various Thymeleaf scenarios, such as generating random numbers in Halo CMS themes.

This article is also available in: Simplified Chinese
此文章有简体中文版本

Integers

Generate random integer in range [min, min+range)

${#numbers.formatDecimal(T(java.lang.Math).floor(T(java.lang.Math).random()*range+min),0,0)}

Generate random integer in range [min, max]

${#numbers.formatDecimal(T(java.lang.Math).floor(T(java.lang.Math).random()*(max-min+1)+min),0,0)}
Click to view examples

A <p> tag displaying an integer between [1, 6) ([1, 5]). Possible results: 1, 2, 3, 4, 5.

Method 1

<p
  th:with="range=5,min=1"
  th:text="${#numbers.formatDecimal(T(java.lang.Math).floor(T(java.lang.Math).random()*range+min),0,0)}"
></p>

Method 2

<p
  th:with="max=5,min=1"
  th:text="${#numbers.formatDecimal(T(java.lang.Math).floor(T(java.lang.Math).random()*(max-min+1)+min),0,0)}"
></p>

Using as List Index

No need for #numbers.formatDecimal when used as list index. Replace my_list with your actual list variable:

Click to view example
<p
  th:with="randomIndex=${T(java.lang.Math).floor(T(java.lang.Math).random()*#lists.size(my_list))}"
  th:text="${my_list[randomIndex]}"
></p>

Formatting Control

Formatted random integer in [min, min+range)
Format: Integer part ≥ integerDigits (padded with leading zeros), decimal part fixed to decimalDigits.

${#numbers.formatDecimal(T(java.lang.Math).floor(T(java.lang.Math).random()*range+min),integerDigits,decimalDigits)}

Formatted random integer in [min, max]
Same formatting rules as above:

${#numbers.formatDecimal(T(java.lang.Math).floor(T(java.lang.Math).random()*(max-min+1)+min),integerDigits,decimalDigits)}
Click to view examples

A <p> tag displaying an integer between [0, 5) ([0, 4]). Possible results: 000.00, 001.00, 002.00, etc.

Method 1

<p 
  th:with="range=5,min=0,integerDigits=3,decimalDigits=2"
  th:text="${#numbers.formatDecimal(T(java.lang.Math).floor(T(java.lang.Math).random()*range+min),integerDigits,decimalDigits)}"
></p>

Method 2

<p 
  th:with="max=4,min=0,integerDigits=3,decimalDigits=2"
  th:text="${#numbers.formatDecimal(T(java.lang.Math).floor(T(java.lang.Math).random()*(max-min+1)+min),integerDigits,decimalDigits)}"
></p>

Decimals/Floats

Generate random float in [min, max)

${T(java.lang.Math).random()*(max-min)+min}
Click to view example

A <p> tag displaying a float between [0.5, 1.8). Possible results: 1.46833041859471749, 0.5578399752996518908, etc.

<p
  th:with="max=1.8,min=0.5"
  th:text="${T(java.lang.Math).random()*(max-min)+min}"
></p>

Formatting Control

Formatted random float in [min, max)
Format: Integer part ≥ integerDigits (padded with leading zeros), decimal part fixed to decimalDigits.

${#numbers.formatDecimal(T(java.lang.Math).random()*(max-min)+min,integerDigits,decimalDigits)}
Click to view example

A <p> tag displaying a float between [0.5, 150.8). Possible results: 00.83, 70.22, 150.23, etc.

<p
  th:with="max=150.8,min=0.5,integerDigits=2,decimalDigits=2"
  th:text="${#numbers.formatDecimal(T(java.lang.Math).random()*(max-min)+min,integerDigits,decimalDigits)}"
></p>

Project Status

As of publication (February 20, 2025), the Thymeleaf random number feature request remains open with no updates:
Random integer in #numbers · Issue #787 · thymeleaf/thymeleaf


0
上一篇 如何用 AI 全方面辅助博客创作 - 从博文之内到博文之外
下一篇 Thymeleaf 随机数生成与格式化详解