rewrote random generation