changed rand method to thread safe