fixed random number generation. simplified code.