mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
parent
9cd8fbb332
commit
e40b9fbbcf
5 changed files with 71 additions and 6 deletions
44
docs/content/en/functions/math/Rand.md
Normal file
44
docs/content/en/functions/math/Rand.md
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
---
|
||||||
|
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]
|
||||||
|
---
|
||||||
|
|
||||||
|
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
|
|
@ -147,6 +147,16 @@ A numeric data type without a fractional component. For example, `42`.
|
||||||
|
|
||||||
Software design and development efforts that enable [localization](#localization). See the [W3C definition](https://www.w3.org/International/questions/qa-i18n). Abbreviated i18n.
|
Software design and development efforts that enable [localization](#localization). See the [W3C definition](https://www.w3.org/International/questions/qa-i18n). Abbreviated i18n.
|
||||||
|
|
||||||
|
###### interval
|
||||||
|
|
||||||
|
An [interval](https://en.wikipedia.org/wiki/Interval_(mathematics)) is a range of numbers between two endpoints: closed, open, or half-open.
|
||||||
|
|
||||||
|
- A _closed_ interval, denoted by brackets, includes its endpoints. For example, [0, 1] is the interval where `0 <= x <= 1`.
|
||||||
|
|
||||||
|
- An _open_ interval, denoted by parenthesis, excludes its endpoints. For example, (0, 1) is the interval where `0 < x < 1`.
|
||||||
|
|
||||||
|
- A _half-open_ interval includes only one of its endpoints. For example, (0, 1] is the _left-open_ interval where `0 < x <= 1`, while [0, 1) is the _right-open_ interval where `0 <= x < 1`.
|
||||||
|
|
||||||
###### kind
|
###### kind
|
||||||
|
|
||||||
See [page kind](#page-kind).
|
See [page kind](#page-kind).
|
||||||
|
|
|
@ -115,6 +115,13 @@ func init() {
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
ns.AddMethodMapping(ctx.Rand,
|
||||||
|
nil,
|
||||||
|
[][2]string{
|
||||||
|
{"{{ math.Rand }}", "0.6312770459590062"},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
ns.AddMethodMapping(ctx.Round,
|
ns.AddMethodMapping(ctx.Round,
|
||||||
nil,
|
nil,
|
||||||
[][2]string{
|
[][2]string{
|
||||||
|
|
|
@ -18,6 +18,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
|
"math/rand"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
|
@ -157,6 +158,11 @@ func (ns *Namespace) Pow(n1, n2 any) (float64, error) {
|
||||||
return math.Pow(af, bf), nil
|
return math.Pow(af, bf), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Rand returns, as a float64, a pseudo-random number in the half-open interval [0.0,1.0).
|
||||||
|
func (ns *Namespace) Rand() float64 {
|
||||||
|
return rand.Float64()
|
||||||
|
}
|
||||||
|
|
||||||
// Round returns the integer nearest to n, rounding half away from zero.
|
// Round returns the integer nearest to n, rounding half away from zero.
|
||||||
func (ns *Namespace) Round(n any) (float64, error) {
|
func (ns *Namespace) Round(n any) (float64, error) {
|
||||||
xf, err := cast.ToFloat64E(n)
|
xf, err := cast.ToFloat64E(n)
|
||||||
|
|
|
@ -61,12 +61,10 @@ title: "**BatMan**"
|
||||||
ns := nsf(d)
|
ns := nsf(d)
|
||||||
for _, mm := range ns.MethodMappings {
|
for _, mm := range ns.MethodMappings {
|
||||||
for _, example := range mm.Examples {
|
for _, example := range mm.Examples {
|
||||||
if strings.Contains(example[0], "errorf") {
|
// These will fail the build, so skip.
|
||||||
// This will fail the build, so skip for now.
|
if strings.Contains(example[0], "errorf") ||
|
||||||
continue
|
strings.Contains(example[0], "transform.XMLEscape") ||
|
||||||
}
|
strings.Contains(example[0], "math.Rand") {
|
||||||
if strings.Contains(example[0], "transform.XMLEscape") {
|
|
||||||
// This will fail the build, so skip for now.
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
templates = append(templates, example[0])
|
templates = append(templates, example[0])
|
||||||
|
|
Loading…
Reference in a new issue