Skip to content

Instantly share code, notes, and snippets.

@georgemarrows
Last active December 29, 2015 03:39
Show Gist options
  • Save georgemarrows/7608804 to your computer and use it in GitHub Desktop.
Save georgemarrows/7608804 to your computer and use it in GitHub Desktop.
See hash_clash.rb for a description
# Produces a histogram illustrating key collisions.
# A round consists of repeatedly drawing a value from 0..255
# until we first see a duplicate value. We count how many draws
# were needed to hit that first duplicate.
# Repeat for 1 million rounds, then draw a histogram showing
# num draws vs num rounds with that num draws.
#
# English escapes me - just read the code!
#
# Written to illustrate comments in this article
# http://nerds-central.blogspot.co.uk/2013/10/ordered-vs-unorderedmap-in-c-some-facts.html
histo = Hash.new(0)
iters = 1_000_000
1.upto(iters) do
set = {} # add to this till we get a clash
i = 0
loop do
i += 1
num = rand(256)
break if set[num] # seen this num before => clash
set[num] = true
end
histo[i] += 1
end
puts "#values\t%age\tGraph"
histo.keys.sort.each do |k|
print k, "\t", "%.2f" % (100*histo[k]/iters.to_f), "\t", "-" * (histo[k]/1000), "\n"
end
$ ruby hash_clash.rb
#values %age Graph
2 0.40 ----
3 0.76 -------
4 1.14 -----------
5 1.51 ---------------
6 1.89 ------------------
7 2.22 ----------------------
8 2.51 -------------------------
9 2.79 ---------------------------
10 3.02 ------------------------------
11 3.28 --------------------------------
12 3.44 ----------------------------------
13 3.62 ------------------------------------
14 3.70 ------------------------------------
15 3.81 --------------------------------------
16 3.84 --------------------------------------
17 3.89 --------------------------------------
18 3.83 --------------------------------------
19 3.78 -------------------------------------
20 3.74 -------------------------------------
21 3.64 ------------------------------------
22 3.54 -----------------------------------
23 3.43 ----------------------------------
24 3.23 --------------------------------
25 3.07 ------------------------------
26 2.95 -----------------------------
27 2.75 ---------------------------
28 2.54 -------------------------
29 2.37 -----------------------
30 2.20 ----------------------
31 1.99 -------------------
32 1.86 ------------------
33 1.63 ----------------
34 1.50 --------------
35 1.35 -------------
36 1.21 ------------
37 1.07 ----------
38 0.95 ---------
39 0.81 --------
40 0.72 -------
41 0.64 ------
42 0.54 -----
43 0.46 ----
44 0.40 ---
45 0.34 ---
46 0.29 --
47 0.24 --
48 0.20 --
49 0.16 -
50 0.14 -
51 0.11 -
52 0.10
53 0.07
54 0.07
55 0.05
56 0.04
57 0.03
58 0.03
59 0.02
60 0.02
61 0.01
62 0.01
63 0.01
64 0.01
65 0.00
66 0.00
67 0.00
68 0.00
69 0.00
70 0.00
71 0.00
72 0.00
73 0.00
74 0.00
76 0.00
77 0.00
80 0.00
81 0.00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment