You cannot access local method variables from outside the method itself:
def my_method
@val1 = 1
val2 = 2
end
my_method #=> 2 returns the value of the last line of the method
@val1 #=> 1 (or you get nil unless you call my_method before)
val2 #=> undefined local variable or method `val2`
Check this post Difference between various variables scopes in ruby
You could rewrite your method like this:
def random_method
return rand(1..25) if rand(2) == 1
0 # return value unless the condition above
end
Note that it doesn't assure that 50% of the times returns a number between 1 and 25, but the probability is 50%.
Then use Kernel#loop for the iteration, you need to decrement x:
x = 99
loop do
puts "x = #{x}"
x -= random_method # decrement x by the value returned by random_method
break if x <= 0
end