| 1 |
--- |
| 2 |
title: collections.Shuffle |
| 3 |
description: Returns a slice by randomizing the element order of the given slice. |
| 4 |
categories: [] |
| 5 |
keywords: [random] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [shuffle] |
| 9 |
returnType: '[]any' |
| 10 |
signatures: [collections.Shuffle SLICE] |
| 11 |
aliases: [/functions/shuffle] |
| 12 |
--- |
| 13 |
|
| 14 |
```go-html-template |
| 15 |
{{ collections.Shuffle (slice "a" "b" "c") }} → [b a c] |
| 16 |
``` |
| 17 |
|
| 18 |
The result will vary from one build to the next. |
| 19 |
|
| 20 |
To render an unordered list of 5 random pages from a page collection: |
| 21 |
|
| 22 |
```go-html-template |
| 23 |
<ul> |
| 24 |
{{ $p := site.RegularPages }} |
| 25 |
{{ range $p | collections.Shuffle | first 5 }} |
| 26 |
<li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li> |
| 27 |
{{ end }} |
| 28 |
</ul> |
| 29 |
``` |
| 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/ |