Random Variable- How to make the bounds variables instead of constants

● ARCHIVED · READ-ONLY
Started by ZiyaBeast 8 posts View original ↗
  1. I noticed that you can set a variable to a random constant that is between one constant and another. For example, I can set the variable “x” to a random number between 1-100.

    My problem is that I would like to set a variable to a random constant between two other variables. For example, I want to set the variable “x” to a random number between variable “y” and variable “z”. If variable y is 2 and variable z is 11 then “x” would be a random number between 2 and 11.

    Does anyone know if there’s a plugin out there that will let me do this? Or is this already possible without plugins?
  2. no need for a plugin or script - this is either done by eventing alone or by a script line.

    Unfortunately you didn't say which maker you're using, and have posted in the wrong forum (this is for getting the editors to work, not for game project help).

    By eventing it can be done with a number of mathematical operations and multiply commands, but that is a complex series of half a dozen commands.
    Better you explain which maker you're using and wait until you get the script command for that maker.
  3. You can do this with a script command

    Code:
    var vr1 = $gameVariables.value(id_of_variable_1);
    var vr2 = $gameVariables.value(id_of_variable_2);
    var rd  = Math.random();
    var value = Math.min(vr1, vr2) + rd * Math.abs(vr2 - vr2);
    
    $gameVariables.setValue(id_of_variable_3, value);

    Your variable y is the variable 1 in my example,
    your variable z is the variable 2 in my example,
    your variable x is the variable 3 in my example.

    This script will set the variable x to a random value between variable y and z. Note that this will only work for RPG Maker MV
  4. Or if your engine is RGSS / VXA or below
    Code:
    a = $game_variables[id1]
    b = $game_variables[id2]
    $game_variables[id3] = a + rand(b - a)
    Yeah why not provide both solutions
  5. @ZiyaBeast Please post which engine you are using.
  6. MushroomCake28 said:
    You can do this with a script command

    Code:
    var vr1 = $gameVariables.value(id_of_variable_1);
    var vr2 = $gameVariables.value(id_of_variable_2);
    var rd  = Math.random();
    var value = Math.min(vr1, vr2) + rd * Math.abs(vr2 - vr2);
    
    $gameVariables.setValue(id_of_variable_3, value);

    Your variable y is the variable 1 in my example,
    your variable z is the variable 2 in my example,
    your variable x is the variable 3 in my example.

    This script will set the variable x to a random value between variable y and z. Note that this will only work for RPG Maker MV

    I had the same the same question. I tried this out using 1 as the low and 20 as the high and I keep getting "1" every time. what am I missing?


    ◆Control Variables:#0001 Low = 1
    ◆Control Variables:#0002 High = 20
    ◆Script:var vr1 = $gameVariables.value(1);
    :Script:var vr2 = $gameVariables.value(2);
    :Script:var rd = Math.random();
    :Script:var value = Math.min(vr1, vr2) + rd * Math.abs(vr2 - vr2);
    :Script:
    :Script:$gameVariables.setValue(3, value);
    ◆Text:None, Window, Bottom
    :Text:\V[3]
  7. CynicSyndrome said:
    what am I missing?
    from the way you wrote that I guess you placed every part of a single script into different script commands.

    the script box is a sandbox, all local variable are erased when the entire box closes.

    Additionally, you always multiply rd with zero because you wrote vr2-vr2 inside the abs, and that always results in zero.
  8. As @Andar mentioned, vr2 - vr2 is zero, and therefore "Math.min(vr1, vr2) + rd * Math.abs(vr2 - vr2);" will evaluate to the minimum of vr1 and vr2 - in your case "1".

    Extra Detail
    This is the breakdown when variables are added in:
    Math.min(vr1, vr2) + rd * Math.abs(vr2 - vr2);
    becomes
    Math.min(1, 20) + [random decimal between 0 and 1] * Math.abs(0);
    becomes
    1 + [random decimal between 0 and 1] * 0;
    becomes
    1 + 0
    which equals 1

    I think you can fix this by changing Math.abs(vr2 - vr2) to Math.abs(vr2 - vr1)

    Additionally, @CynicSyndrome, please do not "necro" old support threads to ask questions, even if you're asking about a solution that has been tried in one of those old threads. Often people see the thread at the top of the forum, and assume the first post is new. Instead, create a new thread to ask your question, and if you think it will help, link to the old support thread where the problem similar to yours was discussed. If your problem isn't solved by the fix I suggested here, you can create a new thread in Learning JavaScript for this.

    Closing this thread now to prevent confusion.