RPG Maker MV
CXJ Core - Random v1.0.0

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, 847 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.

Methods

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

Constructor

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.

Arguments:

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

CXJScripts.CXJCore.Random.prototype.setSeed(seed)

Sets the seed of a Random object

Arguments:

seedoptional A random seed

CXJScripts.CXJCore.Random.prototype.getSeed()

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).

Arguments:

num1optional A minimum number
num2optional A maximum number

Returns: An integer


CXJScripts.CXJCore.Random.prototype.nextRandom()

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

Returns: A float between 0 and 1

/******************************************************************************
 * CXJ_CXJCoreRandom.js                                                       *
 ******************************************************************************
 * CXJ Core - Random v1.0.0                                                   *
 * By G.A.M. Kertopermono, a.k.a. GaryCXJk                                    *
 ******************************************************************************
 * License: ISC                                                               *
 ******************************************************************************
 * Copyright (c) 2017, G.A.M. Kertopermono                                    *
 *                                                                            *
 * Permission to use, copy, modify, and/or distribute this software for any   *
 * purpose with or without fee is hereby granted, provided that the above     *
 * copyright notice and this permission notice appear in all copies.          *
 *                                                                            *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES   *
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF           *
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR    *
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES     *
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN      *
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR *
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.                *
 ******************************************************************************/
 
/*:
 * @plugindesc A random number generator
 * @author G.A.M. Kertopermono
 *
 * @help
 * ============================================================================
 * = About                                                                    =
 * ============================================================================
 *
 * 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.
 *
 * ============================================================================
 * = Usage                                                                    =
 * ============================================================================
 * 
 * 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 CXJCore 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.
 * 
 * ----------------------------------------------------------------------------
 * - Methods                                                                  -
 * ----------------------------------------------------------------------------
 * 
 * CXJScripts.CXJCore.Random(seed, bigInt1, bigInt2, bigIntMod)
 * 
 * Constructor.
 * 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.
 * 
 * Arguments:
 * 
 * seed      - (optional) A random seed
 * bigInt1   - (optional) An integer, preferably a prime number
 * bigInt2   - (optional) An integer, preferably a prime number
 * bigIntMod - (optional) An integer, preferably a prime number, and at least
 *             bigger than (the multiplication of) the previous two integers
 *
 * ---
 *
 * CXJScripts.CXJCore.Random.prototype.setSeed(seed)
 * 
 * Sets the seed of a Random object
 * 
 * Arguments:
 * 
 * seed      - A random seed
 * 
 * ---
 * 
 * CXJScripts.CXJCore.Random.prototype.getSeed()
 * 
 * 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).
 * 
 * Arguments:
 * 
 * num1      - (optional) A minimum number
 * num2      - (optional) A maximum number
 * 
 * Returns: An integer
 * 
 * ---
 * 
 * CXJScripts.CXJCore.Random.prototype.nextRandom()
 * 
 * Generates a random float between 0 (inclusive) and 1 (inclusive)
 * 
 * Returns: A float between 0 and 1
 * 
 * ============================================================================
 * = Compatibility                                                            =
 * ============================================================================
 * 
 * This plugin does not overwrite default functionality.
 *
 * ============================================================================
 * = Changelog                                                                =
 * ============================================================================
 *
 * 1.0.0 (2017-07-15)
 * ------------------
 *
 * * Initial release
 *
 * ============================================================================
 * = License                                                                  =
 * ============================================================================
 *
 * Copyright (c) 2017, G.A.M. Kertopermono
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * ============================================================================
 */
 
// If it doesn't already exist, create a new CXJScripts object.
/** @namespace */
var CXJScripts = CXJScripts || {};

+function(_) {
    // Creates the CXJCore object and attaches it to the main CXJScripts object
    var CXJCore = {};
    _.CXJCore = CXJCore;
    
    /**
     * @constructor
     * 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.
     * @param {int} seed
     * @param {int} bigInt1
     * @param {int} bigInt2
     * @param {int} bigIntMod
     * @returns {CXJScripts.CXJCore.Random}
     */
    CXJCore.Random = function(seed, bigInt1, bigInt2, bigIntMod) {
        if(!seed && seed !== 0) {
            seed = (new Date()).getTime();
        }
        this._bigInt1 = 13;
        this._bigInt2 = 271;
        this._bigIntMod = 0x5A1ADDAD;
        if(bigInt1 || bigInt1 === 0) {
            this._bigInt1 = bigInt1;
        }
        if(bigInt2 || bigInt2 === 0) {
            this._bigInt2 = bigInt2;
        }
        if(bigIntMod || bigIntMod === 0) {
            this._bigIntMod = bigIntMod;
        }
        this.setSeed(seed);
    }

    /**
     * Sets the seed of a Random object
     * @param {type} seed
     */
    CXJCore.Random.prototype.setSeed = function(seed) {
        this._seed = seed;
    }

    /**
     * Gets the current seed of a Random object
     * @returns {int}
     */
    CXJCore.Random.prototype.getSeed = function() {
        return this._seed;
    }

    /**
     * Sets a new random seed
     * @private
     */
    CXJCore.Random.prototype._next = function() {
        this._seed = (this._seed * this._bigInt1 + this._bigInt2) % this._bigIntMod;
    }

    /**
     * Generates a random integer. Optionally, you can define a range, between
     * the lowest number (inclusive) and the highest (exclusive).
     * @param {int} num1
     * @param {int} num2
     * @returns {int}
     */
    CXJCore.Random.prototype.nextInt = function(num1, num2) {
        this._next();
        var num = this._seed;
        if(num1 !== null && num1 !== undefined && ((num2 !== null && num2 !== undefined) || num1 > 0)) {
            if(num2 === null || num2 === undefined) {
                num2 = 0;
            }
            if(num2 > num1) {
                var num3 = num2;
                num2 = num1;
                num1 = num3;
            }
            num = (num % (num1 - num2)) + num2;
        }
        return num;
    }

    /**
     * Generates a random float between 0 (inclusive) and 1 (inclusive)
     * @returns {float}
     */
    CXJCore.Random.prototype.nextRandom = function() {
        this._next();
        return this._seed / (this._bigIntMod - 1);
    }
}(CXJScripts);                                

Creator: GaryCXJk

Release date: 2017-07-15

Downloads: 847

License: ISC License

Optional compatibility: