I created a small scripts that takes a lottery number of length x and loops until it finds a match. I created it just for learning purposes and out of curiosity of how many loops are needed to find a match.
At the moment it prints the number of loops to the terminal but I would prefer if it only printed every 5000 or so tries.
I put an if expression that checks the see if loop_num / 5000 ends in 0, in the cond for each non match but I get the following error:
"elixir elixirPlay.exs
** (SyntaxError) elixirPlay.exs:83: unexpected token: ). The “do” at line 77 is missing terminator “end”
HINT: it looks like the "do" on line 1 does not have a matching "end"
(elixir) lib/code.ex:813: Code.require_file/2
"
If I comment out the if it works again. Are if expressions allowed in conds? What’s the solution?
Any other other critiques on the code?
defmodule Play do
def random_numbered_balls num do
(for _x <- 1..num, do: Enum.random 1..49) |> List.to_tuple
end
def loto(true), do: IO.puts "We have a match!"
def loto(num_of_balls), do: loto(random_numbered_balls(num_of_balls), num_of_balls, 0)
def loto(static_balls_list, num_of_balls, loop_num) do
static_balls = static_balls_list
changing_balls = random_numbered_balls(num_of_balls)
cond do
(static_balls == changing_balls) ->
IO.inspect static_balls
IO.inspect changing_balls
loto(true)
(static_balls != changing_balls) ->
#if (String.ends_with?(to_string(loop_num / 5000)), "0")), do: IO.puts "#{loop_num} tries"
IO.puts "#{loop_num} tries" #(temp) would prefer to print only ever 5000 tries but can't get the above to work
loop_num = (loop_num + 1)
loto(static_balls, num_of_balls, loop_num)
end
end
end #module end
Play.loto(x)