Uh, you can't just drop in 'random_r' in place of 'random'. If you do that, what will happen is that you'll get the same sequence of random numbers in every thread, making the whole multithreading exercise useless!
You have to be sure to seed the generator differently in each thread, before calling 'random_r'.