I have been playing around with Hamming Weight of late. Simply, with a Ulong of all reset bits the Hamming Weight is zero. With a Ulong of all set bits the Hamming Weight is 32. With "01101100010011110110101110010011", for example, we have a Hamming Weight of 18, the number of set bits.
The combinations of ones in a Ulong form a binomial distribution so the average Hamming Weight will be 16.
It occurred to me that this would be a good way to check a PRNG's distribution uniformity on the 32-bit outputs.
Running six tests of 10^8 passes per PRNG and taking the average, I get this.
Code: Select all
CryptoRndII 16.00002775
PCG32II 16.00006797
MsWs 16.00016002
Squares 15.99972075
SFC32 15.99967304
I did a similar exercise with PowerBASIC's RND and it got 15.5. I think PB's RND is a LCG and it may be a variant which only outputs odd numbers. That would explain 15.5, 31/2. With some applications, that may not be an issue. However, its period is only 2^32, it fails PractRand at 4KB and is not particularly fast. Needless to say, I stopped using it a long time ago and why I wrote CryptoRndII.dll and PCG32.dll - not just for PB's members but for me, as well.
CryptoRndII and PC32II are ideal for Monte Carlo work, remembering that CryptoRndII is Windows-based.
GetHammingWeight
Code: Select all
Function GetHammingWeight( ByVal GivenDW As Ulong ) As long
Asm
mov eax, Dword Ptr GivenDW
xor ecx, ecx ' Number of ones
0:
test eax, eax
jz 1f
mov edx, eax
dec edx
and eax, edx
inc ecx
jmp 0b
1:
mov [Function], ecx
End Asm
End Function