Created
August 31, 2012 03:58
-
-
Save mckelvin/3548927 to your computer and use it in GitHub Desktop.
random_hamming_dist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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