Control Variables - How to randomize while leaving out certain numbers

KrimsonKatt

The Endless
Veteran
Joined
Nov 12, 2016
Messages
271
Reaction score
162
First Language
English
Primarily Uses
RMMV
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.
 

Cootadude

Noob with exp
Veteran
Joined
May 3, 2019
Messages
44
Reaction score
35
First Language
English
Primarily Uses
RMMV
I'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.
 

estriole

Veteran
Veteran
Joined
Jun 27, 2012
Messages
1,447
Reaction score
692
First Language
indonesian
change variable -> script:
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.
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,366
Reaction score
1,777
First Language
EN
Primarily Uses
RMMZ
:kaohi: @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:
JavaScript:
var a = [1,2,3,4,5,10]; a[Math.randomInt(a.length)]
This requires you to manually input the numbers, as estriole mentions. In this example the numbers are 1, 2, 3, 4, 5, and 10.
 

estriole

Veteran
Veteran
Joined
Jun 27, 2012
Messages
1,447
Reaction score
692
First Language
indonesian
:kaohi: @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:
JavaScript:
var a = [1,2,3,4,5,10]; a[Math.randomInt(a.length)]
This requires you to manually input the numbers, as estriole mentions. In this example the numbers are 1, 2, 3, 4, 5, and 10.
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
 
Last edited:

Latest Threads

Latest Posts

Latest Profile Posts

My Game Development Livestream

Of course, so, someone had covid at work today and I was exposed to them, so, I'm stuck at home for...TWO WEEKS!
oh god I'm going to die
AAAAAAAAi.png
Kind of relieved that I had medibang installed when I need to edit Sprite I was about to download gimp but I remember I had medibang installed lol

Forum statistics

Threads
110,369
Messages
1,052,671
Members
143,403
Latest member
prismat83
Top