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

Property definitions

core :: math $ Collection :: rand
	# 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

core :: math $ SequenceRead :: rand
	# Optimized for large collections using `[]`
	redef fun rand
	do
		assert not is_empty
		return self[length.rand]
	end
lib/core/math.nit:503,2--508,4

pthreads $ ConcurrentCollection :: rand
	redef fun rand
	do
		mutex.lock
		var r = real_collection.rand
		mutex.unlock
		return r
	end
lib/pthreads/concurrent_collections.nit:156,2--162,4