Rekkid

Villager
Member
Joined
Jan 3, 2014
Messages
17
Reaction score
2
First Language
English
Primarily Uses
I would like to request a simple plugin where you can set the min/max value and skew and it will output an integer and save it to X variable. My goal is to make RNG in my game more normalized.

example:
Plugin Command: NormalCurve (min, max, variable)
Plugin Command: NormalCurve (20, 100, 4) would generate a random integer between 20 and 100 and save it to variable 4.
 

Aerosys

Veteran
Veteran
Joined
Apr 23, 2019
Messages
709
Reaction score
685
First Language
german
Primarily Uses
RMMZ
Let me know if it works, I didn't test it as it contains just a few lines of code.

MZ users: Simply do a Plugin Command

MV & MZ users: Do a JS Code and insert
Code:
randomNumber(min, max, variableId)
e.g.
Code:
randomNumber(10, 200, 4)
 

Attachments

  • randomNumber.js
    835 bytes · Views: 5

ScorchedGround

Blizzards most disappointed fan (More than ever)
Veteran
Joined
Apr 12, 2020
Messages
751
Reaction score
1,133
First Language
German
Primarily Uses
RMMV
Just create an empty JS file and stick this in there:

JavaScript:
function rnd_num(param1, param2, variable) {
 
    var min = Math.min(Math.abs(param1),Math.abs(param2));
    var max = Math.max(Math.abs(param1),Math.abs(param2));
 
    var randomNumber = min + Math.randomInt(max+1-min);
 
    $gameVariables.setValue(variable, randomNumber);
 
};

Then you can use the function
JavaScript:
rnd_num(param1, param2, variable);
by using the "script-call" command.

Just put in any two numbers in any order and it will save a random number between the smaller and the bigger number in the desired variable.

edit: ninja'd
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
3,673
Reaction score
2,760
First Language
EN
Primarily Uses
RMMZ
Both of the above solutions are non-skewed/uniform distributions, which is easily done with existing event commands or a simple min + Math.randomInt(max - min + 1) script call.

When referring to a probability distribution, the word "normalised" means that if you multiply every possible output value by its probability over the distribution's domain, the answer is 1 (i.e. 100%). This has nothing to do with the normal (a.k.a. Gaussian) distribution.

Incidentally, the normal distribution is typically used for analysing a data set, rather than generating or skewing random numbers. Hence its characterisation in terms of mean and standard deviation.

You can naively skew a uniform RNG by passing the uniform value through the skew function, e.g. skewing Math.random (uniform on [0, 1)) through f(x) = x²:
JavaScript:
Math.pow(Math.random(), 2)
Then you can do the usual multiplying/rounding to get whatever kind of value you like. However, this does not guarantee that the skewed RNG will be normalised.

I think this represents a Gaussian curve? :kaoswt:
JavaScript:
function gaussian(x, mean, dev) {
    // exp(-(x-μ)²/2σ²) / σ√2π
    return Math.exp(
        -Math.pow((x - mean) / dev, 2) / 2
    ) / (dev * Math.SQRT1_2 / Math.sqrt(Math.PI));
}
So in theory you may be able to use that. I'd suggest using a polynomial or something instead, though. If you want to normalise the Gaussian-skewed result then things get complicated because that curve's integral requires numerical approximation.

(Hopefully I haven't misinterpreted and written that all out for nothing. :kaoslp:)
 

Nolonar

Veteran
Veteran
Joined
Feb 18, 2018
Messages
421
Reaction score
576
First Language
French, German
Primarily Uses
RMMZ
Hey, everyone. Unfortunately, I can't provide a function for normal-distributed random numbers (I'm looking for one myself), however, I've just spent 3 hours making a tool that should help with testing the function itself: https://nolonar.github.io/DistributionGrapher/webapp/

Simply enter a function and click on the "Test function" button to see how the generated numbers are distributed.

For example, following function:
JavaScript:
Math.pow(Math.random(), 2)

will result in the following distribution:
Screenshot 2022-05-21 013558.png

Have fun with the tool, and I hope you'll be able to come up with satisfactory normal-distributed RNGs.


EDIT:
I just found a function that approximates a normal distribution well enough:
Screenshot 2022-05-21 014806.png

It's basically just adding 6 random numbers together.
 
Last edited:

Rekkid

Villager
Member
Joined
Jan 3, 2014
Messages
17
Reaction score
2
First Language
English
Primarily Uses
Thanks everyone I ended up combining parts of everyone's answers to get exactly what I want.
 

Latest Threads

Latest Profile Posts

Messing around with fragment shaders (filters). This thing is HUGE and I'm surprised almost no one makes good use of. Even the number of plugins that do this stuff are limited.
ScreenShot_6_28_2022_3_2_40.png

There's 7 main areas in the game that are all connected. This train get's you from an underground lab to the center of someone's subconscious. inside the train you can talk to some of the passengers. but going to the top of it, there's battles.
Speedrunning all character portraits just so they all have the same quality. Not sure if this was a good idea, but now I'm almost done.
We now have a gameplay video!
DrBuni wrote on Akiritas's profile.
That is a cute Link picture, reminds me of the Samurai Jack artstyle.

Forum statistics

Threads
123,146
Messages
1,154,380
Members
161,494
Latest member
EnceladusSnow
Top