# 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

You have no idea how much time I spent last night trying to get a Santa hat to fit on my avatar's head. Failure. Head's too small.
I realized recently since my game is still a turn based game, that something like the card game Yomi is a lot closer to what I should try to aim for combat wise. Should also help for those who don't really play fighting games to make things a bit easier to understand. ugh the struggles of trying to show love for my 2 favorite genres in 1 go lol.
Did a bit of work on the chemistry tiles I want.

Hey There!

My next plugin will be: "Dynamic Temperature Stats"

Now you might be wondering... What does it do?

Well, it's a fascinating plugin that adds a body temperature mechanic to characters.

Holy crap, it's cold. Our heat went out this morning and the repair guy won't get the part needed to fix it at least until tomorrow. Had to be now of all times, when we have stuff to do all week and it's the coldest night of November so far.