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.
Sunny B. (Guest)
on 2008-12-04 06:14
(Received via mailing list)
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

---

Sunny

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

>> had any luck winning the big jackpot - http://megamillions.com/. So
>>   @a=[]
>>
>> 100000.times do
>>   puts giveMeNumbers
>> 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
in your pocket ;-)

Rob B.    http://agileconsultingllc.com
removed_email_address@domain.invalid
This topic is locked and can not be replied to.