How to generate distinct random numbers per distinct threads in .NET?
- by mark
Dear ladies and sirs.
I have to generate 19 bit random numbers. However, there is a constraint - two threads may not generate the same random number when running certain code.
The simplest solution is lock the entire code. However, I would like to know if there is a non locking solution. I thought, I can incorporate ManagedThreadId within the produced random numbers, but the ManagedThreadId documentation on the Internet mentions that it may span the whole Int32 range. Unmanaged thread id seems to be limited to 11 bits, still this leaves me with just 8 truly random bits.
Are there any other ways? Somehow to utilize the Thread Local Storage, may be?
Thanks.