Top Level Namespace

Defined Under Namespace

Modules: ScatterSwap

Instance Method Summary collapse

Instance Method Details

#visualize_hashObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/scatter_swap/run.rb', line 43

def visualize_hash
  puts "hash"
  40.times do |integer|
    output = "|"
    3.times do |index|
      output += " #{(integer + (123456789 * index)).to_s.rjust(5, ' ')}"
      output += " => #{hashed = ScatterSwap.hash(integer + (123456789 * index) ) }"
      output += " => #{ScatterSwap.reverse_hash(hashed) } |"
    end
    puts output
  end
end

#visualize_scatterObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/scatter_swap/run.rb', line 57

def visualize_scatter
  puts "original      scattered     unscattered"
  20.times do |integer|
    output = ""
    2.times do |index|
      original = ScatterSwap.arrayify(integer + (123456789 * index))
      scattered = ScatterSwap.scatter(original.clone)
      unscattered = ScatterSwap.unscatter(scattered.clone)
      output += "#{original.join}" 
      output += " => #{scattered.join}"
      output += " => #{unscattered.join}    |    "
    end
    puts output
  end
end

#visualize_scatter_and_unscatterObject

This file is spike code and is not part of the library

While developing this, I used this file to visualize what was going on with the numbers

You can uncomment various methods at the bottom and then run it like this:

watch -n1 ruby run_scatter_swap.rb

tweak the code a bit and see instant visual changes in the generated numbers



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/scatter_swap/run.rb', line 13

def visualize_scatter_and_unscatter
  # change this number to experiment with different values
  rotations = 99

  original = ScatterSwap.arrayify(123456789)
  scattered = []
  unscattered = []
  puts original.join
  puts "rotate!(#{rotations})"
  10.times do
    puts original.rotate!(rotations).join + "->" + scattered.push(original.pop).join
  end
  puts "scattered"
  puts scattered.join
  10.times do
    puts unscattered.push(scattered.pop).join + "->" + unscattered.rotate!(rotations * -1).join
  end

  puts unscattered.join
end

#visualize_spinObject

find hash for lots of spins



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/scatter_swap/run.rb', line 75

def visualize_spin
  2000.times do |original|
    hashed_values = []
    9000000000.times do |spin|
      hashed =  ScatterSwap.hash(original, spin)
      if hashed_values.include? hashed
        puts "collision: #{original} - #{spin} - #{hashed}"
        break
      end
      hashed_values.push hashed
    end
  end
end

#visualize_swapper_mapObject



35
36
37
38
39
40
41
# File 'lib/scatter_swap/run.rb', line 35

def visualize_swapper_map
  puts "swapper map"
  10.times do |index|
    key = 1
    puts ScatterSwap.swapper_map(index).join.to_s
  end
end