I want to have 2 variables which point on a random number between 1 and 10, but the 2 variables must point on different values, I don't want both of them to value 5 or another value at the same time. So I wrote this method:
def give_a_value
@num_array.each do |num|
value=1+rand(10)
if value==num
give_a_value
end
end
@num_array.push(value)
value
end
@num_array=[0]
var1=give_a_value
var2=give_a_value
The problem is that it doesn't work. It seems like the program doesn't enter the loop ôO. I've been thinking about this for some days and I still don't find the solution, so I ask for your help.
Try this: ;)
var1 = rand(10) + 1
var2 = var1
while var1 == var2
var2 = rand(10) + 1
end
If it was that simple I would already have found the way ^^. But thank you for having tried.
Actually I forgot to mention that I need to do this for 8 variables (and even more if I decide to increase the values that the variable can take and by the way the number of variables... in another code I'll need it...), by doing like that my code would be way too huge. That's why I need to use a method.
Try this:
vars = Array.new(8, 0)
vars.each_index do |i|
a = 0
while vars.include?(a)
a = rand(10) + 1
end
vars[i] = a
end
That could work, thanks ^^, I didn't already try to puts all my variables into an array. I'm going to test it to see if it doesn't interfere with the rest.