Files
hugo/docs/content/en/functions/collections/D.md
T
2025-12-19 10:07:30 +01:00

2.2 KiB
Raw Blame History

title, description, categories, keywords, params
title description categories keywords params
collections.D Returns a slice of sequentially ordered random integers.
random
functions_and_methods
returnType signatures
[]int
collections.D SEED N HIGH

{{< new-in 0.149.0 />}}

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.

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]

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)

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. 5867, Mar. 1987. ↩︎