Condition: Random 1..Variable

Hi Guys,
This is probably really simple, but I can't figure it out. I'm making a condition branch and rather than using the default random [Number]..[Number], I want to set it to Random 1..Variable[16].

I know I have to do this in the script section but I have no idea how to make this happen. Can anyone advise?

Thanks in advance.
You say conditional branch but that doesn't have a random option. Do you mean you're trying to set a variable to a random number from a range?
Total fail on my part there. Yeah I mean variable setup. (See the image).
Hi there,

You don't necessarily need the script section for this, though it might make things easier. So either of these solutions might work:

- Without using Script:

"Set" your variable to Random 0...<highest number the editor allows>
"Mod" with Variable[16] (this will yield you a random number between 0 and Variable[16] minus 1)
"Add" 1.
This might promote lower values by an additional 1:10000000 chance.
<snip>
Edit: The additional chance is absolute, which might cause problems for large maximums (see post #6 and #7). In these cases, you probably should use the script solution below. 🙂
Thanks to Andar for pointing this out.

- Using Script:
You can generate a random integer by typing
min + Math.randomInt(max - min + 1);
with min/max being 1 / $gameVariables.value(16) respectively.
Hi there,

You don't necessarily need the script section for this, though it might make things easier. So either of these solutions might work:

- Without using Script:

"Set" your variable to Random 0...<highest inputable value>
"Mod" with Variable[16] (this will yield you a random number between 0 and Variable[16] minus 1)
"Add" 1.
This might promote lower values by an additional 1:10000000 chance, but this won't be noticable in almost any case I could think of.

- Using Script:
You can generate a random integer by typing
min + Math.randomInt(max - min + 1);
with min/max being 1 / $gameVariables.value(16) respectively.

Works like a charm. Thanks
@Another Fen
Not exactly, the way with the modulo operation will have an increase in the probability of the lower numbers depending on which numbers are used as maximum and as modulo.

The modulo operation gives the rest after a division by the modulo operator. Let's just assume the maximum is set as 100, and the variable contains an 80
As a result, for any random number between 0-79 the modulo operation will give the number rolled. And for any random number between 80 and 99, the modulo will return 0-19.
That will result in a double chance of getting a result between 0 and 19, and a normal chance of getting a result between 20 and 79.

A double chance is not exactly what I would call "won't be noticable" - it will only be insignificant if the modulo operator is much smaller than the maximum random limit, and in regular game use that is rarely the case in my opinion.
@Andar:
Wooops, yeah, you are completely right on that. Sorry.

Though I might have been a bit unclear in my previous post:
In VXAce, the highest inputable number for the "random" option would be 99,999,999. I'm not sure if MV has a similar limitation (I'd guess so), but using this ranges up to maybe several thousand should be fine in general (case-dependent of course, at 10,000 your lower numbers may already have a 0.1% occurrence bonus compared to the rest).

As you said, this only works if the modulo operator is relatively small, so rolling a number between 1 and 66,666,666 using this method would not be a good idea.
I fear I was a bit too focused on a simple "calculate 1:X" scenario here. 🙂
@Another Fen
Not exactly, the way with the modulo operation will have an increase in the probability of the lower numbers depending on which numbers are used as maximum and as modulo.

The modulo operation gives the rest after a division by the modulo operator. Let's just assume the maximum is set as 100, and the variable contains an 80
As a result, for any random number between 0-79 the modulo operation will give the number rolled. And for any random number between 80 and 99, the modulo will return 0-19.
That will result in a double chance of getting a result between 0 and 19, and a normal chance of getting a result between 20 and 79.

A double chance is not exactly what I would call "won't be noticable" - it will only be insignificant if the modulo operator is much smaller than the maximum random limit, and in regular game use that is rarely the case in my opinion.

Well I have no idea what you just said, i see the words but it's all a bit technical for me lol. The basis of what I was trying to do anyway is to set a variable based on the average number of my party (so 99 max) then as you go along in the game there are traps that can be triggered that will select enemies from my troop list based on the random variable that was chosen, therefore as the player levels up more the traps spawn harder mobs.

This is basically a way to ensure that the traps in each region don't become too easy to complete later in the game and thus the game scales with the players level. So far the script call that was suggested by @Another Fen is working fine.

Original thread: https://forums.rpgmakerweb.com/threads/condition-random-1-variable.76977/