class Object

Public Instance Methods

visualize_hash() click to toggle source
# 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_scatter() click to toggle source
# 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_unscatter() click to toggle source

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

# 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_spin() click to toggle source

find hash for lots of spins

# 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_map() click to toggle source
# 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