Added locking on use of rand() just in case.