With '#define Twin False' I get 87 MHz which is what I would expect of Mersenne Twister on my machine.
With '#define Twin True' I get '16 MHz 16 MHz'.
This is indicative of massive collisions. That 16 MHz varies, sometimes it may be 15 MHz, 20 MHz and so on. So, the throughput has been seriously curtailed. Suppose the primary thread gets a RND update the secondary thread of execution gets nothing and will re-use the last RND it got which will have a profound effect on the distribution and, therefore, the randomness of RND. I have an Intel CPU with 4 cores/4 threads. Running RND in eight threads will see the throughput experienced in each thread drop like a stone and the randomness will be totally shot.
If we replace RND with my PCG32II, which is thread safe, I get 497 MHz with Twin as false and '495 MHz 521 MHz' with Twin as true. OK, the throughput's for Twin as true are not identical, but we are talking a multi-tasking operating system and I very much doubt that a user will notice. Running on eight threads will see about 4000 MHz of random floats being pushed through.
Code: Select all
#define Twin False
'#define Twin True
Dim As Ulong i
Dim As Double t1, y
Dim Shared As Double t2
Dim Shared As Ulong counter
Dim As Any Ptr hThread
Randomize
Sub SecondThread( byval dummy as any ptr)
Dim As Ulong i
Dim As Double y
t2 = Timer
For i = 1 To counter
y = Rnd
Next
t2 = Timer - t2
End Sub
counter = 10^8 ' 100 million
#if Twin
hThread = Threadcreate( @SecondThread, 0 )
#endif
t1 = Timer
For i = 1 To counter
y = Rnd
Next
t1 = Timer - t1
#if Twin
Threadwait( hThread )
#endif
#if Twin
Print Int(100/t1);" MHz", Int(100/t2);" MHz"
#else
Print Int(100/t1);" MHz"
#endif
Sleep