Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Lottery


Author: ntk
License: The MIT License, Copyright (c) 2007 ntk
Inspired by: Lottery Quick Pick
Hints: Lottery mathematics


class Lottery

   def playing(i=1, n1=0, r1=0..0, n2=0, r2=0..0)

      return nil unless i.is_a?(Integer) && n1.is_a?(Integer) && n1 > 0 && r1.is_a?(Range) && n2.is_a?(Integer) && r2.is_a?(Range)
      stats = Hash.new(0)   # optional; stores the frequency of picked numbers, cf. stats[random_num] += 1
      ret = []

      random_procs = [ 
         lambda { |a,n| a.slice!(rand(n)) },      # a is an array, n an integer
         lambda { |a,n| a.slice!(rand(n) * -1) }, 
         lambda { |a,n| a.slice!((rand(n) * -1) + rand(n)) },
         lambda { |a,n| a.slice!((rand(n) - rand(n))) }, 
         lambda { |a,n| a.reverse!.slice!(rand(n)) },
         lambda { |a,n| a.reverse!.slice!(rand(n) * -1) }, 
         lambda { |a,n| a.reverse!.slice!((rand(n) * -1) + rand(n)) },
         lambda { |a,n| a.reverse!.slice!((rand(n) - rand(n))) } 
      ] 

      i.times do
         numbers = r1.to_a.sort_by {rand}
         num = numbers.size
         ar = []
         count = 0

         while count < n1 + n2

            count += 1

            if count > n1 && n2 > 0
               numbers2 = r2.to_a
               numbers2 = (numbers & numbers2).sort_by {rand} 
               num2 = numbers2.size
               count2 = 0
               while count2 < n2
                  count2 += 1
                  i = rand(random_procs.size)   # get a random array index for random_procs array
                  random_procs = random_procs.sort_by {rand}      
                  random_num = random_procs.at(i).call(numbers2, num2)
                  stats[random_num] += 1
                  ar << random_num
                  numbers2 = numbers2.sort_by {rand}
                  num2 -= 1
               end   # while

               if count2 > 1 then count += (count2-1) end  
               #break   # alternative
              
            else

               i = rand(random_procs.size)                               
               random_procs = random_procs.sort_by {rand}  
  
               random_num = random_procs.at(i).call(numbers, num)
               stats[random_num] += 1
               ar << random_num

=begin
              if rand(num) % (rand(num) + 1) == rand(num)               #  alternative
                  random_num = random_procs.at(i).call(numbers, num)
                  stats[random_num] += 1
                  ar << random_num
               else
                  random_num = numbers.slice!(rand(num))
                  stats[random_num] += 1
                  ar << random_num
               end
=end

               numbers = numbers.sort_by {rand}
               num -= 1

            end          # if
         end             # while
         ret << ar
      end
      #ret
      ret << stats   # optional
   end

end


puts
puts "UK National Lottery, German Lotto 6/49, ..."

ndraws = 10
result = Lottery.new.playing(ndraws, 6, 1..49)
stats = result.pop

result.each do |t| 
   puts t.sort.join('-')
end

puts
n = 0
stats.sort.each do |k,v| 
   i = (v / (ndraws * 6.0) ) * 100.0
   n += i
   puts "#{k} :: #{v} :: #{i}%"
end
p n

puts
puts "US Powerball Lottery"
Lottery.new.playing(ndraws, 5, 1..55, 1, 1..42)[0..-2].each do |t| 
   n = t.pop
   puts "#{t.sort.join('-')}  and  #{n}"
end

puts
puts "Mega Millions"
Lottery.new.playing(ndraws, 5, 1..56, 1, 1..46)[0..-2].each do |t| 
   n = t.pop
   puts "#{t.sort.join('-')}  and  #{n}"
end

puts
puts "EuroMillions"
Lottery.new.playing(ndraws, 5, 1..50, 2, 1..9)[0..-2].each do |t| 
   n1 = t.pop
   n2 = t.pop
   puts "#{t.sort.join('-')}  and  #{n1}-#{n2}"
end


puts
puts "US Powerball Lottery"

ndraws = 10
nums = [5, 23, 25, 33, 34]
pb = 31

matches = []
n = 0

Lottery.new.playing(ndraws, 5, 1..55, 1, 1..42)[0..-2].each do |t| 
   i = t.pop
   t = t.sort
   if (t & nums).size > matches.size || ( matches.size == 0 && i == pb )
      matches = t & nums 
      i == pb ? n = i : n = 0
   end
end

if pb == n && !matches.empty?
   puts "Your numbers: #{nums.join('-')} and #{pb}\nYour matches: #{matches.join('-')} and #{n}"
elsif pb == n && matches.empty?
   puts "Your numbers: #{nums.join('-')} and #{pb}\nYour matches: [] and #{n}"
else
   puts "Your numbers: #{nums.join('-')} and #{pb}\nYour matches: #{matches.join('-')}"
end


« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS