Skip to content

Instantly share code, notes, and snippets.

@vasi
Created February 2, 2013 22:17
Show Gist options
  • Save vasi/4699510 to your computer and use it in GitHub Desktop.
Save vasi/4699510 to your computer and use it in GitHub Desktop.
#!/usr/bin/env rvm 1.9.3 do ruby
require 'pp'
def hamming(i)
dist = 0
v = i
while v != 0
dist += 1 if (v & 1 != 0)
v >>= 1
end
dist
end
$alphabet_size = 32
$log = Math.log2($alphabet_size).ceil.to_i
$hammings = []
(0..(1 << $log)).each do |i|
$hammings[i] = hamming(i)
end
def hamming_cached(i)
return $hammings[i]
end
sample = 10 * 1000
choices = 4
hist = [0] * (Math.log2($alphabet_size).ceil + 1)
sample.times do
x = 0
choices.times { x ^= rand($alphabet_size) }
hist[hamming_cached(x)] += 1
end
pp hist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment