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.
EDIT
I wish to clear something. The random numbers may repeat, this is inevitable, eventually. What I wish to avoid is if a certain piece of code is simultaneously entered by two threads, then these threads may not use the same random number, guaranteed. Before or after that code is executed - fine, but not within the code itself. Again, I can use some locking scheme to prevent it, but I want to examine non locking schemes first.
EDIT 2
Reading the comments/answers to my question I have understood that I cannot avoid but to lock the particular code. However, for the sake of pure academic curiosity, I am still interested to know if anyone knows a good solution to my original question - generating 19 bits random numbers in multiple threads, where the numbers are guaranteed to be distinct between threads, given that the ManagedThreadId can be potentially very large, so large that simply aggregating it within the random number is bad - leaves no space for the actual random bits.