# Ruby fun with MegaMillions

**URL:** <https://www.ruby-forum.com/t/ruby-fun-with-megamillions/154813>\
**Category:** Rails\
**Created:** [December 4, 2008, 5:14am UTC](https://www.ruby-forum.com/t/ruby-fun-with-megamillions/154813 "2008-12-04T05:14:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [December 4, 2008, 5:14am UTC](https://www.ruby-forum.com/t/ruby-fun-with-megamillions/154813/1 "2008-12-04T05:14:25Z")

</div>

I thought I would write a lottery number generator since I have not had  
any  
luck winning the big jackpot - [http://megamillions.com/](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.

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [December 4, 2008, 7:54am UTC](https://www.ruby-forum.com/t/ruby-fun-with-megamillions/154813/2 "2008-12-04T07:54:55Z")

</div>

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

Ryan B.  
Freelancer

> **[frozenplague.net](http://frozenplague.net)**
>
> This domain may be for sale!

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [December 4, 2008, 2:31pm UTC](https://www.ruby-forum.com/t/ruby-fun-with-megamillions/154813/3 "2008-12-04T14:31:06Z")

</div>

On Dec 4, 2008, at 1:55 AM, Ryan B. wrote:

> > had any luck winning the big jackpot - [http://megamillions.com/](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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
