A.Vector.take_random
You're seeing just the function
take_random
, go back to A.Vector module for more information.
Specs
take_random(t(val), non_neg_integer()) :: t(val) when val: value()
Takes amount
random elements from vector
.
Note that, unless amount
is 0
or 1
, this function will
traverse the whole vector
to get the random sub-vector.
If amount
is more than the vector
size, this is equivalent to shuffling the vector
:
the returned vector cannot be bigger than the original one.
See Enum.random/1
for notes on implementation and random seed.
Runs in linear time (except for amount <= 1
, which is effective constant time).
Examples
# Although not necessary, let's seed the random algorithm
iex> :rand.seed(:exrop, {1, 2, 3})
iex> A.Vector.new(1..10) |> A.Vector.take_random(2)
vec([7, 2])
iex> A.Vector.new([:foo, :bar, :baz]) |> A.Vector.take_random(100)
vec([:bar, :baz, :foo])