Should this code be using pattern matching?

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

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