mirror of
https://github.com/gohugoio/hugo.git
synced 2026-08-24 23:38:53 +00:00
content: Add seed examples to collections.D
This commit is contained in:
@@ -131,6 +131,7 @@ body {
|
||||
}
|
||||
|
||||
/* Code spans within paragraphs. */
|
||||
p > code {
|
||||
p > code,
|
||||
td > code {
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
@@ -11,10 +11,12 @@ params:
|
||||
|
||||
{{< new-in 0.149.0 />}}
|
||||
|
||||
The `collections.D` function returns a slice of `N` sequentially ordered unique random integers in the half-open [interval](g) [0, `HIGH`) using the provided `SEED` value. This function implements J. S. Vitter's Method D[^1] for sequential random sampling, a fast and efficient algorithm for this task.
|
||||
The `collections.D` function returns a slice of `N` sequentially ordered unique random integers in the half-open [interval](g) [0, `HIGH`) using the provided [`SEED`](g) value. This function implements J. S. Vitter's Method D[^1] for sequential random sampling, a fast and efficient algorithm for this task.
|
||||
|
||||
See [this article][] for a detailed explanation.
|
||||
|
||||
## Examples
|
||||
|
||||
```go-html-template
|
||||
{{ collections.D 6 7 42 }} → [4, 9, 10, 20, 22, 24, 41]
|
||||
```
|
||||
@@ -33,7 +35,7 @@ A common use case is the selection of random pages from a page collection. For e
|
||||
```go-html-template
|
||||
<ul>
|
||||
{{ $p := site.RegularPages }}
|
||||
{{ range collections.D now.YearDay 5 ($p | len) }}
|
||||
{{ range collections.D time.Now.YearDay 5 ($p | len) }}
|
||||
{{ with (index $p .) }}
|
||||
<li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
|
||||
{{ end }}
|
||||
@@ -43,11 +45,23 @@ A common use case is the selection of random pages from a page collection. For e
|
||||
|
||||
The construct above is significantly faster than using the [`collections.Shuffle`][] function.
|
||||
|
||||
## Seed value
|
||||
|
||||
Choosing an appropriate seed value depends on your objective.
|
||||
|
||||
Objective|Seed example
|
||||
:--|:--
|
||||
Consistent result|`42`
|
||||
Different result on each call|`time.Now.UnixNano`
|
||||
Same result per day|`time.Now.YearDay`
|
||||
Same result per page|`hash.FNV32a .Path`
|
||||
Different result per page per day|`hash.FNV32a (print .Path time.Now.YearDay)`
|
||||
|
||||
> [!note]
|
||||
> The slice created by this function is limited to 1 million elements.
|
||||
|
||||
[^1]: J. S. Vitter, "An efficient algorithm for sequential random sampling," _ACM Trans. Math. Soft._, vol. 13, pp. 58–67, Mar. 1987.
|
||||
|
||||
[this article]: https://getkerf.wordpress.com/2016/03/30/the-best-algorithm-no-one-knows-about/
|
||||
[`collections.Shuffle`]: /functions/collections/shuffle/
|
||||
[day of the year]: /methods/time/yearday/
|
||||
[this article]: https://getkerf.wordpress.com/2016/03/30/the-best-algorithm-no-one-knows-about/
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
---
|
||||
title: seed
|
||||
reference: https://en.wikipedia.org/wiki/Random_seed
|
||||
---
|
||||
|
||||
A _seed_ is the starting point for a computer algorithm that generates pseudo-random numbers. Using the same seed will always produce the identical sequence of numbers, which is essential for reproducibility in areas like simulations, cryptography, and video games.
|
||||
Reference in New Issue
Block a user