Skip to content

Instantly share code, notes, and snippets.

@jonelf
Last active October 3, 2017 21:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonelf/3030a7d343f691348c68 to your computer and use it in GitHub Desktop.
Save jonelf/3030a7d343f691348c68 to your computer and use it in GitHub Desktop.
A "random" FizzBuzz in Ruby. This is just for fun.
c = ('a'..'z').to_a
srand(1792579 * 1800)
f = (0..3).map { c[rand(26)] }
b = (0..3).map { c[rand(26)] }
a = [f, b].map {|s| s.join.capitalize }
srand(s = 93113 * 19243)
puts (0..99).map{|i| [a[0], a[0] + a[(srand(s) if i%15 < 1) || 1].to_s, a[1], i+1][rand(4)] }
@jonelf
Copy link
Author

jonelf commented Apr 29, 2015

This is one way to find the seed for an arbitrary string.

chars=('a'..'z').to_a
(0..999999999999).each do |n|
  srand(n)
  if chars[rand(26)] == "f" && chars[rand(26)] == "i" && chars[rand(26)] == "z" && chars[rand(26)] == "z"
    puts n.to_s
  end
end

@jonelf
Copy link
Author

jonelf commented Apr 29, 2015

FizzBuzz repeats itself every 15th step and this is how to find such a sequence.

require 'pp'
(0..999999999999999).each do |n|
  srand(n)
  a = (0..14).map { rand(4) }
  b = [a[1], a[3], a[6], a[7], a[10], a[12], a[13]]
  c = [a[5], a[8], a[11]]

  if a[4] == a[9] && b.all? {|n| n == a[0]} && c.all? {|n| n == a[2]} && [a[0], a[2], a[4]].uniq.length == 3
    puts n.to_s
    pp a
    break
  end
end

The array we are searching for looks like this
[A, A, B, A, C, B, A, A, B, C, A, B, A, A, D]

@jonelf
Copy link
Author

jonelf commented Aug 2, 2017

a=%w[Fizz ☹ Buzz]
a[1]=a[0]+a[2]
srand(1791773459)
((0..14).map{rand(4)}*7)[0..99].each_with_index{|v,i|puts a[v]||i+1}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment