How to generate distinct random numbers per distinct threads in .NET?

Posted by mark on Stack Overflow See other posts from Stack Overflow or by mark
Published on 2010-03-28T15:52:52Z Indexed on 2010/03/28 16:03 UTC
Read the original article Hit count: 142

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about multithreading