HUGO
Menu
GitHub 90265 stars Mastodon

math.Rand

Returns a pseudo-random number in the half-open interval [0.0, 1.0).
Syntax
math.Rand
Returns
float64

Usage

The math.Rand function returns a pseudo-random number in the half-open interval [0.0, 1.0). Combine it with other functions to generate random integers or floats within a given interval, as shown in the examples.

Examples

{{ math.Rand }} → 0.6312770459590062

A random integer in the closed interval [0, 5]:

{{ math.Rand | mul 6 | math.Floor }}

A random integer in the closed interval [1, 6]:

{{ math.Rand | mul 6 | math.Ceil }}

A random float, with one digit after the decimal point, in the closed interval [0, 4.9]:

{{ div (math.Rand | mul 50 | math.Floor) 10 }}

A random float, with one digit after the decimal point, in the closed interval [0.1, 5.0]:

{{ div (math.Rand | mul 50 | math.Ceil) 10 }}