Forum: Ruby on Rails Ruby fun with MegaMillions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
on 2008-12-04 05:14
```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:

@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
end

---

Sunny

ps. The next step is to calculate what my winnings would  be based on
the
payout table - for non-jackpot winners.```
on 2008-12-04 07:54
```I estimate you need to run this at least 550,731,822 times in order to
get the same numbers twice.
-----
Ryan Bigg
Freelancer
http://frozenplague.net```
on 2008-12-04 14:31
```On Dec 4, 2008, at 1:55 AM, Ryan Bigg wrote:

>> had any luck winning the big jackpot - http://megamillions.com/. So
>>   @a=[]
>>
>> 100000.times do
>> end
>>
>> ---
>>
>> Sunny
>>
>> ps. The next step is to calculate what my winnings would  be based
>> on the payout table - for non-jackpot winners.

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