Fix rand() and replace the use of randi.