Multiply variable by a range

Lammaka

Villager
Hello! I' m trying to multiply a variable by a number between x and y with a scrpt. Let's say x = 50 and y = 200. I tried with this one:

\$gameParty.gainGold(\$gameVariables.value(52) * >= 50 && <= 200);

but is not working. Any ideas?

AquaEcho

Script Kitty
Try this
\$gameParty.gainGold(\$gameVariables.value(52) * (Math.floor(Math.random() * 151) + 50))

Lammaka

Villager
It works! Thanks! However, as I have to repeat this process with other values, I would like to understand the math behind that formula. I mean, what should I do if I want to change the values? Why 151?

AquaEcho

Script Kitty
Math.random() gives you a random decimal number between 0 inclusive and 1 exclusive or 0<=x<1.
(Math.random() * 151) gives you a random decimal number between 0 inclusive and 151 exclusive or 0<=x<151, so 0 to 150.
Math.floor will floor the result or round it down so it's an integer/whole number.
+50 will add 50 to the result and ensure a minimum value of 50.
So your result is a random integer/whole number between 50 and 200 inclusive.

Last edited:
Bex

Lammaka

Villager
Ohh! I undestood! You're great. Thank you so much.

ATT_Turan

Forewarner of the Black Wind
If it's easier, you can use the RPG Maker function Math.randomInt(X).

It will generate a number between 0 and X-1.

It's doing exactly the same thing behind the scenes as what @AquaEcho gave you, but it can be easier to read and work with.

Latest Profile Posts

So the concept for my Game Jam project is way more than I can complete before the deadline. But I think I found a way to get the core done and make the incomplete parts either not really noticeable or make them a part of the story for now. That sneaky, dastardly Krampus! Then I can complete the rest. Did I mention that this is fun?
I've been working on a game called "Eternal Sunset". Is there anywhere here i can "show it off" maybe? Wondering what others think.
What do you do when there are lots of offers online but you don't have even a slight chance of ever playing those myriads of games?