Fixed bug where rand-generator sometimes went outside range