Broken rand function does not work well