| 1 |
--- |
| 2 |
title: math.Rand |
| 3 |
description: Returns a pseudo-random number in the half-open interval [0.0, 1.0). |
| 4 |
categories: [] |
| 5 |
keywords: [random] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [] |
| 9 |
returnType: float64 |
| 10 |
signatures: [math.Rand] |
| 11 |
--- |
| 12 |
|
| 13 |
The `math.Rand` function returns a pseudo-random number in the half-open [interval](g) [0.0, 1.0). |
| 14 |
|
| 15 |
```go-html-template |
| 16 |
{{ math.Rand }} → 0.6312770459590062 |
| 17 |
``` |
| 18 |
|
| 19 |
To generate a random integer in the closed interval [0, 5]: |
| 20 |
|
| 21 |
```go-html-template |
| 22 |
{{ math.Rand | mul 6 | math.Floor }} |
| 23 |
``` |
| 24 |
|
| 25 |
To generate a random integer in the closed interval [1, 6]: |
| 26 |
|
| 27 |
```go-html-template |
| 28 |
{{ math.Rand | mul 6 | math.Ceil }} |
| 29 |
``` |
| 30 |
|
| 31 |
To generate a random float, with one digit after the decimal point, in the closed interval [0, 4.9]: |
| 32 |
|
| 33 |
```go-html-template |
| 34 |
{{ div (math.Rand | mul 50 | math.Floor) 10 }} |
| 35 |
``` |
| 36 |
|
| 37 |
To generate a random float, with one digit after the decimal point, in the closed interval [0.1, 5.0]: |
| 38 |
|
| 39 |
```go-html-template |
| 40 |
{{ div (math.Rand | mul 50 | math.Ceil) 10 }} |
| 41 |
``` |