fix some bugs. Added new functions in common for randomise