Optimize? Did you use srand() first to set the seed of the random number generator? Because if you didn't your numbers will be likely predictable...