In my game there's an area which allows you to randomly fight any monster or boss in the game. I have a variable set that is named "arena enemy chosen" which picks a random number from 1 to 346, (the total number of troops in the game) and then that variable is used to determine the troop. Problem is is that there is a large amount of troops I do not want to be called, like empty troops used for barriers and bosses which contain dialogue. (which is most of them) While I have made copies of every dialogue boss without the dialogue, there are still all the problematic troops that I don't want to be called when the variable determining the troop that appear is randomized. I really don't want to re-organize all the troops in my game which would mean going through every map and changing the random encounters, which almost guarantees a glitch occurring since I'll probably miss a couple maps or some the few on-map encounters, causing major problems for my game. The only other way is to have the randomly selected variable automatically not pull up numbers that are assigned to states I don't want playing. Is there a plugin I could use or some in-engine solution for this problem? Also keep in mind that I do not know JavaScript, so unless you meticulously explain every little detail about the code you provide if you do so and what it does, I won't understand a word you say. Thanks.
Control Variables - How to randomize while leaving out certain numbers
RPG Maker MV SupportI'm not sure how you put of your scripts/events.
But maybe you could add a conditional branch that checks if the random number is one you don't want and then have it redo the randomization.
One problem with this is if the game keeps choosing a wrong number it will take some time until the script is complete.
But maybe you could add a conditional branch that checks if the random number is one you don't want and then have it redo the randomization.
One problem with this is if the game keeps choosing a wrong number it will take some time until the script is complete.
change variable -> script:
you see the first bracket [1, 2, 3, 55, 56, 58, 99, 102]
you can modify it to all the troop id you have (separate it by coma).
above mean you will randomize troop id 1, 2, 3, 55, 56, 58, 99, and 102.
so if your troop id is 1, 3, 4, 5, 6, 88, 89, 90, 91, 92
change it to
[1, 3, 4, 5, 6, 88, 89, 90, 91, 92]
don't change any other thing OUTSIDE the first bracket...
hope this help.
Code:
[1 , 2, 3, 55, 56, 58, 99, 102].sort((a,b)=> 0.5 - Math.random())[0]
you see the first bracket [1, 2, 3, 55, 56, 58, 99, 102]
you can modify it to all the troop id you have (separate it by coma).
above mean you will randomize troop id 1, 2, 3, 55, 56, 58, 99, and 102.
so if your troop id is 1, 3, 4, 5, 6, 88, 89, 90, 91, 92
change it to
[1, 3, 4, 5, 6, 88, 89, 90, 91, 92]
don't change any other thing OUTSIDE the first bracket...
hope this help.
👋 @estriole: I think your approach will produce biased results? For an unbiased random sort consider Fisher-Yates. A sort is unnecessary here, though~
To select a random number from a list, I suggest Control Variables > Script:
This requires you to manually input the numbers, as estriole mentions. In this example the numbers are 1, 2, 3, 4, 5, and 10.
To select a random number from a list, I suggest Control Variables > Script:
JavaScript:
var a = [1,2,3,4,5,10]; a[Math.randomInt(a.length)]
Fisher-Yates Shuffle might be too much to use for people who doesn't know javascript though(based on OP)... and might need to write the function in a plugin instead one liner code in control variable script...
but i do agree this kind of randomization produced biased result... but i think for simple randomization it's still okay to use though...
the solution above also plugin free type solution.
edit:
also you're right about the sort also might not needed in this case... the code i suggest is based on another problem which require to grab 10 element from array without any duplicates... i change it to only grab one
Original thread: https://forums.rpgmakerweb.com/threads/control-variables-how-to-randomize-while-leaving-out-certain-numbers.134239/