Ruby fun with MegaMillions

I thought I would write a lottery number generator since I have not had any luck winning the big jackpot - http://megamillions.com/. So far, after over 1.2 million tries, I still have not hit all the numbers from yesterday: 2, 17, 22, 32, 51, 35 (last one is the mega ball).

Fun little project that and I thought others would be interested. Let me know if you have a cleaner implementation in mind.

— code:

def giveMeNumbers @a=

while @a.length <=4 @a << rand(56)+1 @a.uniq! end

@mega = rand(46)+1 @outPut = @a.sort.push(@mega).join(', ')

end

100000.times do puts giveMeNumbers end

I estimate you need to run this at least 550,731,822 times in order to get the same numbers twice.

class MegaMillions

From a set of integers from 1 to numbers, choose count,

plus a single bonus number from 1 to bonus

def initialize(numbers=56, count=5, bonus=46)

@numbers, @count, @bonus = numbers, count, bonus

@balls = (1…@numbers).to_a

@pick = nil

end

def autopick

@pick =

@pick << @balls.sort_by{rand}.first(@count).sort

@pick << rand(@bonus)+1

self

end

def to_s

autopick unless @pick

“#{@pick.first.join(', ')} #{@pick.last}”

end

def inspect

“#<#{self.class.name}:#{‘%#x’%self.object_id} #{@numbers}C#{@count} + #{@bonus}#{%{ => #{to_s}} if @pick}>”

end

end

picker = MegaMillions.new

puts picker.autopick

-Rob

P.S. I think the sweet spot on the payout matrix is to keep the dollar in your pocket :wink:

Rob Biedenharn http://agileconsultingllc.com

Rob@AgileConsultingLLC.com