lib: add an optimized `SequenceRead[E].rand`
[nit.git] / lib / standard / math.nit
index 4b6ed35..14c0e40 100644 (file)
@@ -238,6 +238,15 @@ redef class Collection[ E ]
        end
 end
 
+redef class SequenceRead[E]
+       # Optimized for large collections using `[]`
+       redef fun rand
+       do
+               assert not is_empty
+               return self[length.rand]
+       end
+end
+
 redef class Sys
        init
        do