Win32 compatibility

Please validate these changes, especially in terms of using random numbers.