Skip to content

Instantly share code, notes, and snippets.

@mckelvin
Created August 31, 2012 03:58
Show Gist options
  • Save mckelvin/3548927 to your computer and use it in GitHub Desktop.
Save mckelvin/3548927 to your computer and use it in GitHub Desktop.
random_hamming_dist
#!/usr/bin/python
# -*- coding:UTF-8 -*-
import numpy as np
def main():
random_hamming_dist()
def random_hamming_dist():
def hamming_dist(a, b, bits=32):
x = a ^ b
dist = sum((x >> i & 1) for i in xrange(bits))
return dist
test_size = 20000
half_test_size = 10000
eva = []
ri = np.random.random_integers(0,1<<32,test_size)
for i in xrange(half_test_size):
eva.append(hamming_dist(ri[i], ri[i+half_test_size]))
stat = [0] * 32
for each in eva:
stat[each] +=1
width = 76.
norm = width / np.max(stat)
for i in xrange(32):
print i,':',
print '*' * int(stat[i] * norm),
print ''
'''the result is expect to be something like:
0 :
1 :
2 :
3 :
4 :
5 :
6 :
7 :
8 : *
9 : ***
10 : ******
11 : ******************
12 : ****************************
13 : *****************************************
14 : ************************************************************
15 : *********************************************************************
16 : ****************************************************************************
17 : *********************************************************************
18 : **********************************************************
19 : ******************************************
20 : *****************************
21 : *****************
22 : *******
23 : **
24 : *
25 :
26 :
27 :
28 :
29 :
30 :
31 :
'''
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment