The RPG Maker Resource Kit

RMRK RPG Maker Creation => RPG Maker General => General Scripting => Topic started by: Samael on April 05, 2007, 11:19:50 AM

Title: Assigning a value to a variable
Post by: Samael on April 05, 2007, 11:19:50 AM
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.
Title: Re: Assigning a value to a variable
Post by: Season In The Abyss on April 06, 2007, 04:35:02 PM
Try this:  ;)
var1 = rand(10) + 1
var2 = var1
while var1 == var2
  var2 = rand(10) + 1
end
Title: Re: Assigning a value to a variable
Post by: Samael on April 06, 2007, 10:14:10 PM
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.
Title: Re: Assigning a value to a variable
Post by: Season In The Abyss on April 07, 2007, 03:51:02 AM
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
Title: Re: Assigning a value to a variable
Post by: Samael on April 07, 2007, 06:44:10 AM
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.