RPG Maker MV
CXJ Core - Random

Well, crap. Apparently JavaScript doesn't have a Random class. Sure, I could just grab a random number generator from somewhere, but screw that, let's make one myself!

Now I'm pretty sure this one isn't as random or even psuedo-random as you might want, but it's meant for those who want a simple random number generator that doesn't use the Math.random() function. It also has the ability to set your own random seeds, so there's that.

The Random object is now part of the CXJCore line of scripts. Even though it is part of it, it's still a stand-alone script, and can even be used outside of RPG Maker MV.

Download (11.26 kB, 6 times downloaded)

This is a simple helper plugin, meant for those with a little bit more programming experience. As such, the end-user is supposed to place this as high as possible. As it doesn't touch any JavaScript class, you can freely use it as you want.

Of course, developers are fully authorized to integrate the code into their own scripts, no strings attached. It is adviseable though to alter the _next function so that it generates better random numbers.

You can use this Random object by creating a new CXJScripts.CXJCore.Random instance. If CXJ Core is installed and the Namespace parameter is set, you can also call this object from the chosen namespace.

When creating a Random object instance, you can leave out any parameters, which will pick the random seed based on the current time in milliseconds. Otherwise, the first argument is the random seed. The next four determine the numbers with which it modifies the number. In the code, they're called _bigInt1, _bigInt2 and _bigIntMod, and the formula to generate the next number is:

this._seed = (this._seed * this._bigInt1 + this._bigInt2) % this._bigIntMod;

To actually get a number, you can use one of two instance methods, nextInt and nextRandom.

nextInt returns an integer. When no arguments are passed, it just returns the current seed after a new one is generated. If one argument is given, it generates a number between 0 and the given number. If a second argument is given, it generates a number between the lowest number and the highest number.

nextRandom returns a floating point value between 0 and 1.


CXJScripts.CXJCore.Random(seed, bigInt1, bigInt2, bigIntMod)


This initializes the Random object. You can optionally set the random seed for this object.

The purpose of this Random object is to allow you to store and reuse random number generators, for example, when used in replays and save states. This allows your game to be consistent throughout the entire run of the game for each seed when programmed correctly.


seedoptional A random seed
bigInt1optional An integer, preferably a prime number
bigInt2optional An integer, preferably a prime number
bigIntModoptional An integer, preferably a prime number, and at least bigger than (the multiplication of) the previous two integers


Sets the seed of a Random object


seedoptional A random seed


Gets the current seed of a Random object

Returns: The current seed as an integer

CXJScripts.CXJCore.Random.prototype.nextInt(num1, num2)

Generates a random integer. Optionally, you can define a range, between the lowest number (inclusive) and the highest (exclusive).


num1optional A minimum number
num2optional A maximum number

Returns: An integer


Generates a random float between 0 (inclusive) and 1 (inclusive)

Returns: A float between 0 and 1

Creator: GaryCXJk

Release date: 2017-07-15

Downloads: 6

License: ISC License

Optional compatibility: