Lammaka

Villager
Member
Joined
Nov 21, 2022
Messages
5
Reaction score
0
First Language
Spanish
Primarily Uses
RMMV
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
Regular
Joined
Sep 20, 2021
Messages
2,206
Reaction score
1,615
First Language
English
Primarily Uses
RMMV
Try this
$gameParty.gainGold($gameVariables.value(52) * (Math.floor(Math.random() * 151) + 50))
 

Lammaka

Villager
Member
Joined
Nov 21, 2022
Messages
5
Reaction score
0
First Language
Spanish
Primarily Uses
RMMV
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
Regular
Joined
Sep 20, 2021
Messages
2,206
Reaction score
1,615
First Language
English
Primarily Uses
RMMV
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:
  • Like
Reactions: Bex

Lammaka

Villager
Member
Joined
Nov 21, 2022
Messages
5
Reaction score
0
First Language
Spanish
Primarily Uses
RMMV
Ohh! I undestood! You're great. Thank you so much.
 

ATT_Turan

Forewarner of the Black Wind
Regular
Joined
Jul 2, 2014
Messages
12,448
Reaction score
10,822
First Language
English
Primarily Uses
RMMV
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 Threads

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?
Ugh, mispelled my username.

Forum statistics

Threads
136,542
Messages
1,267,338
Members
180,212
Latest member
cameron_ekiert
Top