Should this code be using pattern matching?

No, not pattern match, guard clause in that case…

https://hexdocs.pm/elixir/1.10.3/Kernel.html#module-guards

But You cannot use random_number inside the guard. You could as well define a module attribute.

def user_guess(guess) when guess == 40, do: IO.inspect("YOU WIN")

etc