Use rand_r() instead of rand().