improved handling of random -- avoid clone bug