Skip to content

Commit a6a9715

Browse files
authored
content: Document the collections.D function
1 parent 66eee71 commit a6a9715

File tree

9 files changed

+72
-6
lines changed

9 files changed

+72
-6
lines changed

‎.cspell.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
"Samsa",
145145
"Stucki",
146146
"Thénardier",
147+
"Vitter",
147148
"WASI",
148149
"# ----------------------------------------------------------------------",
149150
"# cspell: ignore operating systems and software packages",

‎assets/css/styles.css‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,8 @@ body {
129129
text-decoration: none;
130130
padding-left: .0625em;
131131
}
132+
133+
/* Code spans within paragraphs. */
134+
p > code {
135+
white-space: nowrap;
136+
}

‎content/en/_common/installation/04-build-from-source.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ _comment: Do not remove front matter.
77
To build the extended or extended/deploy edition from source you must:
88

99
1. Install [Git]
10-
1. Install [Go] version 1.23.0 or later
10+
1. Install [Go] version 1.24.0 or later
1111
1. Install a C compiler, either [GCC] or [Clang]
1212
1. Update your `PATH` environment variable as described in the [Go documentation]
1313

‎content/en/contribute/development.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ For a complete guide to contributing to Hugo, see the [Contribution Guide].
3232
To build the extended or extended/deploy edition from source you must:
3333

3434
1. Install [Git]
35-
1. Install [Go] version 1.23.0 or later
35+
1. Install [Go] version 1.24.0 or later
3636
1. Install a C compiler, either [GCC] or [Clang]
3737
1. Update your `PATH` environment variable as described in the [Go documentation]
3838

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
title: collections.D
3+
description: Returns a slice of sequentially ordered random integers.
4+
categories: []
5+
keywords: [random]
6+
params:
7+
functions_and_methods:
8+
returnType: '[]int'
9+
signatures: [collections.D SEED N HIGH]
10+
---
11+
12+
{{< new-in 0.149.0 />}}
13+
14+
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&nbsp;D[^1] for sequential random sampling, a fast and efficient algorithm for this task.
15+
16+
See [this article][] for a detailed explanation.
17+
18+
```go-html-template
19+
{{ collections.D 6 7 42 }} → [4, 9, 10, 20, 22, 24, 41]
20+
```
21+
22+
The example above generates the _same_ random numbers each time it is called. To generate a _different_ set of 7 random numbers in the same range, change the seed value.
23+
24+
```go-html-template
25+
{{ collections.D 2 7 42 }} → [3, 11, 19, 25, 32, 33, 38]
26+
```
27+
28+
> [!note]
29+
> All arguments are cast to integers, so setting the seed to `3.14` is the same as setting it to `3`.
30+
31+
A common use case is the selection of random pages from a page collection. For example, to render a list of 5 random pages using the [day of the year][] as the seed value:
32+
33+
```go-html-template
34+
<ul>
35+
{{ $p := site.RegularPages }}
36+
{{ range collections.D now.YearDay 5 ($p | len) }}
37+
{{ with (index $p .) }}
38+
<li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
39+
{{ end }}
40+
{{ end }}
41+
</ul>
42+
```
43+
44+
The construct above is significantly faster than using the [`collections.Shuffle`][] function.
45+
46+
> [!note]
47+
> The slice created by this function is limited to 1 million elements.
48+
49+
[^1]: J. S. Vitter, "An efficient algorithm for sequential random sampling," _ACM Trans. Math. Soft._, vol. 13, pp. 58&ndash;67, Mar. 1987.
50+
51+
[this article]: https://getkerf.wordpress.com/2016/03/30/the-best-algorithm-no-one-knows-about/
52+
[`collections.Shuffle`]: /functions/collections/shuffle/
53+
[day of the year]: /methods/time/yearday/

‎content/en/functions/collections/Seq.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ A contrived example of iterating over a sequence of integers:
3232
```
3333

3434
> [!note]
35-
> The slice created by the `seq` function is limited to 2000 elements.
35+
> The slice created by this function is limited to 1 million elements.

‎content/en/functions/collections/Shuffle.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: collections.Shuffle
33
description: Returns a random permutation of a given array or slice.
44
categories: []
5-
keywords: []
5+
keywords: [random]
66
params:
77
functions_and_methods:
88
aliases: [shuffle]
@@ -27,3 +27,9 @@ To render an unordered list of 5 random pages from a page collection:
2727
{{ end }}
2828
</ul>
2929
```
30+
31+
{{< new-in 0.149.0 />}}
32+
33+
Using the [`collections.D`][] function for the same task is significantly faster.
34+
35+
[`collections.D`]: /functions/collections/D/

‎content/en/functions/math/Rand.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: math.Rand
33
description: Returns a pseudo-random number in the half-open interval [0.0, 1.0).
44
categories: []
5-
keywords: []
5+
keywords: [random]
66
params:
77
functions_and_methods:
88
aliases: []

‎data/keywords.yaml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# will decrease over time, though the initial implementation will require some
88
# effort.
99

10+
- highlight
1011
- menu
12+
- random
1113
- resource
12-
- highlight

0 commit comments

Comments
 (0)