I can instead create a FAQ about my code.
Q: Why do you use random() instead of first 8 bytes of SHA256 of the transaction hash as described in the algorithm?
A: Because SHA256 is designed to generate values that are uniformly distributed.
Q: What happens if a==b? Why don't you process this situation?
A: The original algorithm described in wiki doesn't give the answer either. But the possibility of this event is proportional to (2
-64)
2 (because 8 first bytes of sha256 have 64 bits) which is about 10
-38 so it is safe in this code to simple skip such cases for clarity. If you are feeling pedantic you can add the line
print NBLOCKS-(na+nb)
in the end of the code and it will always (well, ok, with an estimated probability of 1-NBLOCKS*(10
-38) = 1-10
-32) give you zero.