Added random and weightedRandom for array elements

Some ideas from ticket #519 and others minors fixes