Set a Variable's value to a random number from 0 to the value of another Variable

Newquaza 96

Veteran
Veteran
Joined
Sep 5, 2014
Messages
35
Reaction score
6
First Language
Italian
Primarily Uses
The question is basically all in the title, but i will explain the context in case there's a better way to do this that i am overlooking.
For my project, i am using stat checks to determine the outcome of certain events:
For example, when a boulder blocks the path, the player will have to choose one of the actors to try and move said boulder
Choosing an actor makes it so the Variable "Calc1" is set to the selected actor's Strength, while the Variable "Calc2" is set to the selected actor's Level+5.
Then, i would change the variable "Random1"'s value to a random number between 0 and the value of the variable "Calc2".
Afterwards, i would have the game check whether the value of the variable "Random1" is smaller than or equal to the value of variable "Calc1".
If Random1 is smaller than or equal to Calc1, then the check is a success. Otherwise, it is a failure.
I tried calling the following script to change the value of variable 1 (Random1) to a number from 0 to the value of variable 16 (Calc2), but it doesn't work.
$gameVariables.setValue(1, Math.random($gameVariables.Value(16)));
Thanks in advance for your help.
 

Rukiri

I like to make Action-RPGs
Veteran
Joined
Jan 20, 2014
Messages
843
Reaction score
513
First Language
English
Primarily Uses
Other
I'm going to say your variable ids are 1 and 2 for this example.
Code:
function random_range(min, max) {
return Math.random() * (max - min) + min;
}
$game_variables[3] = random_range($game_variables[1], $game_variables[2]);
if ($game_variables[3] <= $game_variables[2]) {
//success
} else {
//failure
}
 

Verdelite

Wannabe Developer
Veteran
Joined
May 22, 2016
Messages
138
Reaction score
1,023
First Language
German
Primarily Uses
RMMV
I'm not entirely sure since I'm not familiar with RMMV's functions yet, but afaik Math.random() returns a decimal, and I think it has to be
$gameVariables.value(id) with a lowercase 'v'
Try using
$gameVariables.setValue(1, Math.randomInt($gameVariables.value(16)));

Feel free to correct me if I'm wrong, am noob :)
 

Newquaza 96

Veteran
Veteran
Joined
Sep 5, 2014
Messages
35
Reaction score
6
First Language
Italian
Primarily Uses
I'm not entirely sure since I'm not familiar with RMMV's functions yet, but afaik Math.random() returns a decimal, and I think it has to be
$gameVariables.value(id) with a lowercase 'v'
Try using


Feel free to correct me if I'm wrong, am noob :)
This actually works! I suppose it was just a matter of small things after all. Thank y'all very much for your help, i greatly appreciate it.
 

Latest Threads

Latest Profile Posts

Snarkyfork wrote on Trihan's profile.
Hey man, I saw you mention that you do MV javascript work? Just wanted to confirm it before I put you on speed dial. =)
"Man is made by his belief. As he believes, so he is."-Krishna
FastFood in my country is neither fast nor cheap like it's expected to be, but people still buy a lot of it. I don't want to imagine how much of it people would eat if it was.
Started finishing up my bosses and characters...I don't know how long...but its going to take long...

I ended up needing to do a chapter one world map remake. It's still WIP but coming along nicely. =D

Forum statistics

Threads
107,757
Messages
1,031,934
Members
139,900
Latest member
CGI_ERICYT
Top