Skip to content

Instantly share code, notes, and snippets.

@rdammkoehler
Forked from murphybytes/eight-ball.rb
Created March 29, 2012 22:14
Show Gist options
  • Save rdammkoehler/2244283 to your computer and use it in GitHub Desktop.
Save rdammkoehler/2244283 to your computer and use it in GitHub Desktop.
Rich and John made a magic 8 ball using Vert.x ... and yeah could have used sample and the array but no-worky
require('vertx')
include Vertx
@server = HttpServer.new
@server.request_handler do | req |
premonition = [ 'Yes.',
'Yes, definitely.',
'As I see it yes.',
'It is certain.',
'Without a doubt.',
'It is decidedly so.',
'You may rely on it.',
'Outlook good.',
'Most likely',
'Signs point to yes.',
'My reply is no.',
'My sources say no.',
'Very doubtful.',
'Outlook not so good.',
'Dont count on it.',
'Cannot predict now',
'Ask again later.',
'Better not tell you now.',
'Cannot predict now.',
'Reply hazy. Try again.',
'Concentrate and ask again.' ]
req.response.end( premonition[ (rand * premonition.size).to_i ] )
end.listen( 8080, 'localhost' )
@rdammkoehler
Copy link
Author

As a curious note; we tested the distribution of the numbers returned by rand over 10,000,000 iterations; pretty good distribution;

premonition = [ 'Yes.', 'Yes, definitely.', 'As I see it yes.', 'It is certain.', 'Without a doubt.', 'It is decidedly so.', 'You may rely on it.', 'Outlook good.', 'Most likely', 'Signs point to yes.', 'My reply is no.', 'My sources say no.', 'Very doubtful.', 'Outlook not so good.', 'Dont count on it.', 'Cannot predict now', 'Ask again later.', 'Better not tell you now.', 'Cannot predict now.', 'Reply hazy. Try again.', 'Concentrate and ask again.' ]

distribution = {}
distribution.default = 0
(0..10000000).each { |i| distribution[(rand * premonition.size).to_i] += 1 }
max_val = distribution.values.sort.last
longest = premonition.sort { |a,b| a.length <=> b.length }.last.length
distribution.keys.collect { |key| [ key, distribution[key] ] }.sort { |a, b| a[1] <=> b[1] }.each { |element| puts "%#{longest}s" % premonition[element[0]] + " => " + ('+' * ((element[1].to_f / max_val.to_f) * 40)) + " (#{element[1]})" }

harmonia 8-ball ruby eightball.rb

  Better not tell you now. => +++++++++++++++++++++++++++++++++++++++ (474798)
    Reply hazy. Try again. => +++++++++++++++++++++++++++++++++++++++ (475248)
       You may rely on it. => +++++++++++++++++++++++++++++++++++++++ (475593)
          Ask again later. => +++++++++++++++++++++++++++++++++++++++ (475596)
               Most likely => +++++++++++++++++++++++++++++++++++++++ (475624)
          As I see it yes. => +++++++++++++++++++++++++++++++++++++++ (475712)
      Outlook not so good. => +++++++++++++++++++++++++++++++++++++++ (475974)
           My reply is no. => +++++++++++++++++++++++++++++++++++++++ (475981)
            It is certain. => +++++++++++++++++++++++++++++++++++++++ (476006)
       It is decidedly so. => +++++++++++++++++++++++++++++++++++++++ (476171)
                      Yes. => +++++++++++++++++++++++++++++++++++++++ (476273)
        Cannot predict now => +++++++++++++++++++++++++++++++++++++++ (476316)
             Outlook good. => +++++++++++++++++++++++++++++++++++++++ (476335)
       Cannot predict now. => +++++++++++++++++++++++++++++++++++++++ (476394)
          Yes, definitely. => +++++++++++++++++++++++++++++++++++++++ (476438)
            Very doubtful. => +++++++++++++++++++++++++++++++++++++++ (476538)
Concentrate and ask again. => +++++++++++++++++++++++++++++++++++++++ (476543)
         Dont count on it. => +++++++++++++++++++++++++++++++++++++++ (476777)
        My sources say no. => +++++++++++++++++++++++++++++++++++++++ (476997)
       Signs point to yes. => +++++++++++++++++++++++++++++++++++++++ (477183)
          Without a doubt. => ++++++++++++++++++++++++++++++++++++++++ (477504)

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