Save-Point
Spread range over variables - Printable Version

+- Save-Point (https://www.save-point.org)
+-- Forum: Archives (https://www.save-point.org/forum-105.html)
+--- Forum: Creation Asylum Archives (https://www.save-point.org/forum-90.html)
+---- Forum: Scripts & Code Snippets (https://www.save-point.org/forum-92.html)
+----- Forum: RPG Maker XP Code (https://www.save-point.org/forum-93.html)
+------ Forum: Code Snippets & Others/Misc (https://www.save-point.org/forum-100.html)
+------ Thread: Spread range over variables (/thread-6612.html)



Spread range over variables - GubiD - 03-22-2009

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