Lottery generation script

Hello everyone!

So, I have this code:

#ticket database
ticket1 = 88795 #ticket number
quantity1 = 3 #quantity bought

puts “\n” #break

#lottery draw
list = []
(list << ‘%05i’ % (rand(99999)+1)).uniq! while list.length < 5
number1, number2, number3, number4, number5 = list
serie1, serie2, serie3, serie4, serie5 = Array.new(5) { rand(180)+1 }
number6 = number1.to_i - 1
number7 = number1.to_i + 1
number8 = number2.to_i - 1
number9 = number2.to_i + 1
number10 = number3.to_i - 1
number11 = number3.to_i + 1
number12 = number1.to_i / 100
number13 = number2.to_i / 100
number14 = number3.to_i / 100
number15 = number4.to_i / 100
number16 = number5.to_i / 100
number17 = number1.to_i % 10
puts list

puts “\n” #break

#winnings
if ticket1 == number1.to_i
prize1 = 400000 * quantity1
puts “Ticket ##{ticket1.to_s.rjust(5, ‘0’)} won €#{prize1}.”
end
if ticket1 == number2.to_i
prize2 = 125000 * quantity1
puts “Ticket ##{ticket1.to_s.rjust(5, ‘0’)} won €#{prize2}.”
end
if ticket1 == number3.to_i
prize3 = 50000 * quantity1
puts “Ticket ##{ticket1.to_s.rjust(5, ‘0’)} won €#{prize3}.”
end
if ticket1 == number4.to_i
prize4 = 20000 * quantity1
puts “Ticket ##{ticket1.to_s.rjust(5, ‘0’)} won €#{prize4}.”
end
if ticket1 == number5.to_i
prize5 = 20000 * quantity1
puts “Ticket ##{ticket1.to_s.rjust(5, ‘0’)} won €#{prize5}.”
end
if ticket1 == number6.to_i or
ticket1 == number7.to_i
prize6 = 2000 * quantity1
puts “Ticket ##{ticket1.to_s.rjust(5, ‘0’)} won €#{prize6}.”
end
if ticket1 == number8.to_i or
ticket1 == number9.to_i
prize7 = 1250 * quantity1
puts “Ticket ##{ticket1.to_s.rjust(5, ‘0’)} won €#{prize7}.”
end
if ticket1 == number10.to_i or
ticket1 == number11.to_i
prize8 = 960 * quantity1
puts “Ticket ##{ticket1.to_s.rjust(5, ‘0’)} won €#{prize8}.”
end
if ticket1.to_i / 100 == number12.to_i or
ticket1.to_i / 100 == number13.to_i or
ticket1.to_i / 100 == number14.to_i or
ticket1.to_i / 100 == number15.to_i or
ticket1.to_i / 100 == number16.to_i
prize9 = 100 * quantity1
puts “Ticket ##{ticket1.to_s.rjust(5, ‘0’)} won €#{prize9}.”
end
if ticket1.to_i % 10 == number17.to_i
prize10 = 20 * quantity1
puts “Ticket ##{ticket1.to_s.rjust(5, ‘0’)} won €#{prize10}.”
end

As you see, despite the lottery draw generator, code is not good at all.
It works only for one ticket, is long and very manual.
My question here is - can anyone help me to come up with a improvement,
where I
*generate a random amount of tickets with quantities 1-10;
*check if any of those tickets win any of the prizes (mentioned in
code above);
*calculates the winnings by multiplying prize money with the
quantity of ticket?

You can begin by trying to catch the repetitions at the code and
substitute them with a method call.

For example:
You repeat this line too much.

puts “Ticket ##{ticket1.to_s.rjust(5, ‘0’)} won #{prize8}.”

Would you find it interesting if this line could be something like…

show_prize(ticket1, prize8)

Wouldn’t it be more “readable”, more maintainable.
And less prone to error (so that if you want to change the code it may
be changed in one place (not in several)).

And… the whole block of code…

if ticket1 == number4.to_i
prize4 = 20000 * quantity1
puts “Ticket ##{ticket1.to_s.rjust(5, ‘0’)} won #{prize4}.”
end

is a repetition… can you figure out how to encapsulate it?

I don’t see much advantage in using ‘number6’ instead of using and
array called number and its index.
Like in number[6]. I think it could be easier to improve code like this.

Show us the code as you progress and I bet more tips will come.

You can fire up a gist (https://gist.github.com/) or even a complete
gitgub repo.
This will be a good exercise.

Abinoam Jr.

So, I updated the coda a bit -
#ticket database
ticket1 = 23453 #ticket number
quantity1 = 3 #quantity bought

puts “\n” #break

#lottery draw
list = []
(list << ‘%05i’ % (rand(99999)+1)).uniq! while list.length < 5
number1, number2, number3, number4, number5 = list
serie1, serie2, serie3, serie4, serie5 = Array.new(5) { rand(180)+1 }
number6 = number1.to_i - 1
number7 = number1.to_i + 1
number8 = number2.to_i - 1
number9 = number2.to_i + 1
number10 = number3.to_i - 1
number11 = number3.to_i + 1
number12 = number1.to_i / 100
number13 = number2.to_i / 100
number14 = number3.to_i / 100
number15 = number4.to_i / 100
number16 = number5.to_i / 100
number17 = number1.to_i % 10
puts list

puts “\n” #break

#prizes
prize1 = 400000 * quantity1 #1st
prize2 = 125000 * quantity1 #2n
prize3 = 50000 * quantity1 #3rd
prize4 = 20000 * quantity1 #4th
prize5 = 2000 * quantity1 #Consolation1
prize6 = 1250 * quantity1 #Consolation2
prize7 = 960 * quantity1 #Consolation3
prize8 = 100 * quantity1 #Cantena
prize9 = 20 * quantity1 #Reintegro

#winnings
def show_results(a, b)
puts “Ticket ##{a.to_s.rjust(5, ‘0’)} won €#{b}.”
end

if ticket1 == number1.to_i
show_results(ticket1, prize1)
end

if ticket1 == number2.to_i
show_results(ticket1, prize2)
end

if ticket1 == number3.to_i
show_results(ticket1, prize3)
end

if ticket1 == number4.to_i or
ticket1 == number5.to_i
show_results(ticket1, prize4)
end

if ticket1 == number6.to_i or
ticket1 == number7.to_i
show_results(ticket1, prize5)
end

if ticket1 == number8.to_i or
ticket1 == number9.to_i
show_results(ticket1, prize6)
end

if ticket1 == number10.to_i or
ticket1 == number11.to_i
show_results(ticket1, prize7)
end

if ticket1.to_i / 100 == number12.to_i or
ticket1.to_i / 100 == number13.to_i or
ticket1.to_i / 100 == number14.to_i or
ticket1.to_i / 100 == number15.to_i or
ticket1.to_i / 100 == number16.to_i
show_results(ticket1, prize8)
end

if ticket1.to_i % 10 == number17.to_i
show_results(ticket1, prize9)
end

But the suggestion I really need is -
How to get it work with 10 (or 20, 50, 99999 randomly generated tickets
with random quantities each)?

I’ve played a little more with your code.

https://github.com/abinoam/reralottery/blob/master/lottery.rb

I’ll touch it again when I have more free time.

You can see the commits individually at
https://github.com/abinoam/reralottery/commits/master

Abinoam Jr.

Hi Re Ra,

I understand your anxiety to see the result you’re searching for.
But, your code is not well structured (IMHO).
This makes it difficult to “evolute” to what you want.

For satisfying your needs you can (“uglyly”) do like bellow.
I tried to touch your code the less I could, so I tried to preserve
all the variable names, and add only the necessary lines to make it
work.

But… there’s much room for improvement.

Basically I added…

tkt_amount = rand(10) + 1 # (1…10)

tkt_amount.times do

ticket1 = rand(99999)+1
quantity1 = rand(10)+1

The complete code is bellow.
Is that what you wanted to do?

#!/usr/bin/env ruby
#coding: utf-8

#ticket database
ticket1 = 23453 #ticket number
quantity1 = 3 #quantity bought

puts “\n” #break

#lottery draw
list = []
(list << ‘%05i’ % (rand(99999)+1)).uniq! while list.length < 5
number1, number2, number3, number4, number5 = list
serie1, serie2, serie3, serie4, serie5 = Array.new(5) { rand(180)+1 }
number6 = number1.to_i - 1
number7 = number1.to_i + 1
number8 = number2.to_i - 1
number9 = number2.to_i + 1
number10 = number3.to_i - 1
number11 = number3.to_i + 1
number12 = number1.to_i / 100
number13 = number2.to_i / 100
number14 = number3.to_i / 100
number15 = number4.to_i / 100
number16 = number5.to_i / 100
number17 = number1.to_i % 10
puts list

puts “\n” #break

#winnings
def show_results(a, b)
puts “Ticket ##{a.to_s.rjust(5, ‘0’)} won #{b}.”
end

tkt_amount = rand(10) + 1 # (1…10)

tkt_amount.times do

ticket1 = rand(99999)+1
quantity1 = rand(10)+1

#prizes
prize1 = 400000 * quantity1 #1st
prize2 = 125000 * quantity1 #2n
prize3 = 50000 * quantity1 #3rd
prize4 = 20000 * quantity1 #4th
prize5 = 2000 * quantity1 #Consolation1
prize6 = 1250 * quantity1 #Consolation2
prize7 = 960 * quantity1 #Consolation3
prize8 = 100 * quantity1 #Cantena
prize9 = 20 * quantity1 #Reintegro

if ticket1 == number1.to_i
show_results(ticket1, prize1)
end

if ticket1 == number2.to_i
show_results(ticket1, prize2)
end

if ticket1 == number3.to_i
show_results(ticket1, prize3)
end

if ticket1 == number4.to_i or
ticket1 == number5.to_i
show_results(ticket1, prize4)
end

if ticket1 == number6.to_i or
ticket1 == number7.to_i
show_results(ticket1, prize5)
end

if ticket1 == number8.to_i or
ticket1 == number9.to_i
show_results(ticket1, prize6)
end

if ticket1 == number10.to_i or
ticket1 == number11.to_i
show_results(ticket1, prize7)
end

if ticket1.to_i / 100 == number12.to_i or
ticket1.to_i / 100 == number13.to_i or
ticket1.to_i / 100 == number14.to_i or
ticket1.to_i / 100 == number15.to_i or
ticket1.to_i / 100 == number16.to_i
show_results(ticket1, prize8)
end

if ticket1.to_i % 10 == number17.to_i
show_results(ticket1, prize9)
end
end

Was this what you wanted?

Abinoam Jr.

I found this interesting so I thought I’d have a go at using OOP and DRY
on this problem. I tried to stick as close to the original functionality
as possible without limiting the scope. File attached.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs