collections.D
Syntax
Returns
The collections.D
function returns a slice of N
sequentially ordered unique random integers in the half-open interval [0, HIGH
) using the provided SEED
value. This function implements J. S. Vitter’s Method D1 for sequential random sampling, a fast and efficient algorithm for this task.
See this article for a detailed explanation.
{{ collections.D 6 7 42 }} → [4, 9, 10, 20, 22, 24, 41]
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.
{{ collections.D 2 7 42 }} → [3, 11, 19, 25, 32, 33, 38]
All arguments are cast to integers, so setting the seed to 3.14
is the same as setting it to 3
.
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:
<ul>
{{ $p := site.RegularPages }}
{{ range collections.D now.YearDay 5 ($p | len) }}
{{ with (index $p .) }}
<li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
{{ end }}
{{ end }}
</ul>
The construct above is significantly faster than using the collections.Shuffle
function.
The slice created by this function is limited to 1 million elements.
J. S. Vitter, “An efficient algorithm for sequential random sampling,” ACM Trans. Math. Soft., vol. 13, pp. 58–67, Mar. 1987. ↩︎