File: Rand.md

package info (click to toggle)
hugo 0.131.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 45,580 kB
  • sloc: javascript: 31,172; xml: 248; makefile: 73; sh: 42
file content (46 lines) | stat: -rw-r--r-- 1,114 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
---
title: math.Rand
description: Returns a pseudo-random number in the half-open interval [0.0, 1.0).
categories: []
keywords: []
action:
  aliases: []
  related: []
  returnType: float64
  signatures: [math.Rand]
---

{{< new-in 0.121.2 >}}

The `math.Rand` function returns a pseudo-random number in the [half-open interval] [0.0, 1.0).

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

To generate a random integer in the [closed interval] [0, 5]:

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

To generate a random integer in the closed interval [1, 6]:

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

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

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

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

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

[closed interval]: /getting-started/glossary/#interval
[half-open interval]: /getting-started/glossary/#interval