Sample Array

Get N random items from an array without replacement.

Code

General
echo "$arr" | jq --argjson n "$n" '[.[] | {val: ., rand: (now | . * 1000000 | floor % 10000)}] | sort_by(.rand) | .[0:$n] | [.[].val]'

Parameters

JSON array

Number of items to sample

Server

More Bash Snippets