make the hash test stronger

This commit is contained in:
Simon Cruanes 2026-03-19 21:16:55 -04:00
parent 4cafc2d2c5
commit cc0679688d
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4

View file

@ -3,17 +3,26 @@
module H64 = CCHash64 module H64 = CCHash64
module XXH = Containers_xxhash module XXH = Containers_xxhash
let n = ref 1_000_000 let n = ref 100_000
let verbose = ref false let verbose = ref false
let check_bit_proba name hash_fn n_samples = let check_bit_proba name hash_fn n_samples =
let rand = Random.State.make [| 42 |] in
let bits = Array.make 64 0 in let bits = Array.make 64 0 in
for i = 1 to n_samples do
let h = hash_fn i in let n_loops = 30 in
for b = 0 to 63 do for _i = 1 to n_loops do
if Int64.(logand h (shift_left 1L b)) <> 0L then bits.(b) <- bits.(b) + 1 let base = Random.State.int64 rand Int64.(pred max_int) |> Int64.to_int in
for i = 1 to n_samples do
let h = hash_fn (base + i) in
for b = 0 to 63 do
if Int64.(logand h (shift_left 1L b)) <> 0L then
bits.(b) <- bits.(b) + 1
done
done done
done; done;
let n_samples = n_loops * n_samples in
if !verbose then ( if !verbose then (
Format.printf "%s bit probabilities after %d samples:@." name n_samples; Format.printf "%s bit probabilities after %d samples:@." name n_samples;
for b = 0 to 63 do for b = 0 to 63 do