Random number from array

Discussion in 'JS Plugin Requests' started by Blackxwolf, Mar 4, 2019.

    Tags:
  1. Blackxwolf

    Blackxwolf Villager Member

    Messages:
    19
    Likes Received:
    35
    First Language:
    Dutch
    Primarily Uses:
    N/A
    Hi, thanks for looking at my thread. The editor only allows ranges (f.e. 1 to 10), and not specific numbers (f.e. 2, 7, and 9), which is very limiting. So, in short, I'd like to have a script that can generate a random number from an array.

    More specifically, what I need is a script that allows me to put a plugin command something like this: random(4, 7, 99, 102). This should generate either number 4, number 7, or number 99, and stores the drawn number in variable 102 (so the last number is the variable it should be stored at). I hope this is clear, I'm not sure how to explain it better. Just to be clear, I really need it to be a plugin command I can call from the editor, not pre-defined arrays from the script.

    I will personally only need to generate a random number from arrays of exactly 10 numbers. However, if you'd like to post this script for others as well, it may be better to leave the length of the array up to the user (this is probably more difficult to code?). The last number should always be the variable number the random number will be stored at.

    Thanks in advance if you can help me! Of course, you will be credited in my game (I plan to upload it here on the forums once it's done).
     
    #1
  2. caethyril

    caethyril ^_^ Veteran

    Messages:
    1,313
    Likes Received:
    818
    Location:
    UK
    First Language:
    English
    Primarily Uses:
    RMMV
    I don't think you need a plugin for this, just a little scripting~

    Control Variables > Set > Script:
    Code:
    var arr = [4, 7, 99]; arr[Math.randomInt(arr.length)]
    I.e. set the variable equal to a random number from the array (4, 7, or 99 in this case). :kaophew:
     
    #2
    Blackxwolf, MushroomCake28 and Shaz like this.
  3. Blackxwolf

    Blackxwolf Villager Member

    Messages:
    19
    Likes Received:
    35
    First Language:
    Dutch
    Primarily Uses:
    N/A
    Thanks, that works like a charm!
     
    #3
    caethyril likes this.
  4. RPG_itch_Studio

    RPG_itch_Studio Veteran Veteran

    Messages:
    74
    Likes Received:
    15
    First Language:
    English
    Primarily Uses:
    RMMV
    @caethyril

    How can I use that array for a second variable WITHOUT the element assigned to the first variable?

    Example:

    var1 = random [4, 7, 99]. Suppose that the result is var1 = 7.

    Now I want var2 = random [4,99]
     
    #4
  5. caethyril

    caethyril ^_^ Veteran

    Messages:
    1,313
    Likes Received:
    818
    Location:
    UK
    First Language:
    English
    Primarily Uses:
    RMMV
    Control Variables is for setting a single variable's value, and the script is sandboxed (the arr variable is deleted after eval). :kaoswt:

    For what you're suggesting I'd suggest a script call instead, e.g.
    Code:
    var arr = [4, 7, 99], vIds = [1, 2];
    for (var n = 0; n < vIds.length; n++) {
      if (arr.length === 0) break;  // no more left
      var ix = Math.randomInt(arr.length);
      $gameVariables.setValue(vIds[n], arr.splice(ix, 1)[0]);
    }
    It'll check each of the vIds variables in order, and for each one take a random element out of the arr array and store it in that variable. :kaopride:
    Here's details on the splice method in case you're interested: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

    If you set vIds = [1, 1], variable 1 will be assigned two values, one after another: the second number selected will overwrite the first. :)
     
    Last edited: Mar 5, 2019
    #5
    RPG_itch_Studio likes this.

Share This Page