Changed  randomization algorithm