RosettaCodeData/Task/Pick-random-element/Jq/pick-random-element-2.jq

12 lines
339 B
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
# Output: a prn in range(0;$n) where $n is `.`
def prn:
if . == 1 then 0
else . as $n
| ([1, (($n-1)|tostring|length)]|max) as $w
| [limit($w; inputs)] | join("") | tonumber
| if . < $n then . else ($n | prn) end
end;
# An illustration - 10 selections at random with replacement:
range(0;10) | ["a", "b", "c"] | .[length|prn]