adding few minor changes and optimisations to the random functions