Files
hugo/docs/content/en/functions/collections/D.md
T
Bjørn Erik Pedersen 30a20122b7 Merge commit '8f3c066d23f431fb2c53d97ea489e4c28b42bd82'
# Conflicts:
#	docs/data/docs.yaml
2026-02-14 12:14:33 +01:00

2.6 KiB

title, description, categories, keywords, params
title description categories keywords params
collections.D Returns a sorted slice of unique random integers based on a given seed, count, and maximum value.
random
functions_and_methods
aliases returnType signatures
[]int
collections.D SEED N HIGH

{{< new-in 0.149.0 />}}

The collections.D function returns a sorted slice of unique random integers in the half-open interval [0, HIGH) using the provided SEED value. The number of elements in the resulting slice is N or HIGH, whichever is less.

  • N and H must be integers in the closed interval [0, 1000000]
  • SEED must be an integer in the closed interval [0, 2^64 - 1]

Return values

Condition Return value
N <= HIGH A sorted random sample of size N using J. S. Vitter's Method D for sequential random sampling
N > HIGH The full, sorted range [0, HIGH) of size HIGH
N == 0 An empty slice
N < 0 Error
N > 10^6 Error
HIGH == 0 An empty slice
HIGH < 0 Error
HIGH > 10^6 Error
SEED < 0 Error
{.no-wrap-first-col}

Examples

{{ 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]

When N is greater than HIGH, this function returns the full, sorted range [0, HIGH) of size HIGH:

{{ collections.D 6 42 7 }} → [0 1 2 3 4 5 6] 

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 time.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.

Seed value

Choosing an appropriate seed value depends on your objective.

Objective Seed example
Consistent result 42
Different result on each call int 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)