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
727
Reaction score
696
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
765
Reaction score
1,157
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,800
Reaction score
2,864
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
423
Reaction score
578
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

Title Screen 100% Finished
Evangeline.png
I'm starting to enjoy doing pixel art! Not to mention it's getting easier to do commissions :kaojoy:
I'm having a surprisingly easy time with the wait for Hogwarts Legacy, though I know other gamers are salivating hard.
:awat:What are you two down there doing?
This status is here just to take up space. And to make you read that.

Forum statistics

Threads
124,358
Messages
1,162,999
Members
163,149
Latest member
garygraythe2nd
Top