Optimized code for speed and accuracy; changed random() to drand48() for speed