rand_r for trees

Here's a purported solution to the `rand_r` problem. I backported the 4.4BSD version, which should also give much better random numbers than the crappy MSVC C library, and the results are now the same on all platforms.

I didn't run the tests.