core :: Collection :: rand
There must be at least one element in the collection
var x = [1,2,3].rand
assert x == 1 or x == 2 or x == 3
# Return a random element form the collection
# There must be at least one element in the collection
#
# ~~~
# var x = [1,2,3].rand
# assert x == 1 or x == 2 or x == 3
# ~~~
fun rand: E
do
if is_empty then abort
var rand_index = length.rand
for e in self do
if rand_index == 0 then return e
rand_index -= 1
end
abort
end
lib/core/math.nit:435,2--452,4