Geeks of the World UNITE! We've created a ruby script to decide who makes the coffee. Literally nano-seconds of fun to be had.
--
#!/usr/bin/env ruby
require "net/http"
require "uri"
class Array
# randomise the shit out of it
def randomize
size.times { insert_at_random pop }
self
end
# insert object into a random location
def insert_at_random *obj
insert(rand(size+1), *obj)
end
end
# Array of people with a quick randomisation
# to make it (possibly) even more random
people = %w(Andrew Mike Ebony).randomize
# Make a paired array for counting totals
totals = people.map { |person| [person, 0] }
# hardcore random number blah:: http://www.randomnumbers.info/content/Press.htm
uri = URI.parse("http://www.randomnumbers.info/cgibin/wqrng.cgi?limit=#{people.length - 1}&amount=100")
completed = false
while !completed
puts "Fetching random numbers..."
src = Net::HTTP.get(uri)
# Extract the numbers from the HTML source
numbers = src.match(/<hr>(.*?)<\/td>/m)[1].strip
puts numbers
numbers = numbers.split(/\s+/).map { |x| x.to_i }
# Iterate over numbers and increment the totals
numbers.each { |x| totals[x][1] += 1 }
# Check that no two values are the same
# Unique totals should match length of people involved
if totals.map { |person,total| total }.uniq.length == people.length
# Get the max total and loser
person = totals.max { |a, b| a[1] <=> b[1] }
puts "And the loser is #{person[0]}"
# ...and we're done!
completed = true
else
puts "Uh oh, there was a tie."
end
end