Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Spread range over variables
#1
Spread range over variables
by GubiD
Mar 22 2009

This is a locked, single-post thread from Creation Asylum. Archived here to prevent its loss.
No support is given.


Aedelstan put forth the request
[quoteAedelstan on Mar 22 2009]Does anyone know how to make a method like this:
var_spread(v1, v2, min, max)
then $game_variables[v1..v2] are each set to a random variable between min and max
This is easy, but I want to have so that each value occurs only once

So if you write
var_spread(1, 3, 4, 6)
Variable #1 would be set to 4 , 5 or 6 (let's say 6)
Variable #2 would be set to 4 or 5 (let's say 4, 6 is already used by var 1)
Variable #3 would be set to 5 (4 and 6 are already used)[/quote]



Here, this should work.

Code:
class Interpreter
  def randomize_vars(start, end, min, max)
    e = end-start
    if e < 0
      e = start-end
      rev_var = true
    end
    v = max-min
    if v < 0
      v = min-max
      rev_rand = true
    end
    #Start assignment
    used = []
    strt = rev_var ? end : start
    base = rev_rand ? max : min
    for i in 0...e
      val = rand(v)
      if !used.include?(val)
        $game_variables[strt+i] = base+val
      end
    end
  end
end
}




Users browsing this thread: