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
753
Reaction score
1,136
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,675
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 Posts

Latest Profile Posts

I made a menu option that pulls a (accurate) version of the world map from my game instead of having a "traditional" world map. How did I do?

1656552400614.png

..Every time after I edit it and repost it I find a new tiny error I made. :kaodes:
1656550526125.png
Town Design is the best part of an RPG. Well, that and dungeon design
I think that this forum's member posts should include their Phone Number and Social Insurance Number.
My PC just broke with everything I've been working on, on it. I can't afford to replace it. Life is just wonderful....
More musics! Praise be to the winter towns!

Forum statistics

Threads
123,178
Messages
1,154,653
Members
161,548
Latest member
laurenht17
Top