# math.Rand
> 返回半开区间 [0.0, 1.0) 内的伪随机数。
- 官方英文原文：https://gohugo.io/functions/math/rand/
- 本页规范地址：https://hugozh.cn/functions/math/rand/
- 最近更新：2026-10-02
- 最后提交：912b1d3 chore(site): 添加 static/CNAME（hugozh.cn），供 GitHub Pages 等平台绑定自定义域名
- 签名：math.Rand
- 返回类型：float64
- 站点：Hugo 中文文档（https://hugozh.cn/）· 社区维护的非官方中文翻译，如有出入以官方英文原文为准

---
`math.Rand` 函数返回半开区间 `[0.0, 1.0)` 内的伪随机数。

```go-html-template
{{ math.Rand }} → 0.6312770459590062
```

生成闭区间 `[0, 5]` 内的随机整数：

```go-html-template
{{ math.Rand | mul 6 | math.Floor }}
```

生成闭区间 `[1, 6]` 内的随机整数：

```go-html-template
{{ math.Rand | mul 6 | math.Ceil }}
```

生成闭区间 `[0, 4.9]` 内、小数点后保留一位的随机浮点数：

```go-html-template
{{ div (math.Rand | mul 50 | math.Floor) 10 }}
```

生成闭区间 `[0.1, 5.0]` 内、小数点后保留一位的随机浮点数：

```go-html-template
{{ div (math.Rand | mul 50 | math.Ceil) 10 }}
```

