[SOLVED] pc.math.random() vs Math.random()

Hello,
First time posting here.

I’m wondering what the significance of the PlayCanvas math.random() function is. Is there some reason why i should be using this over the standard Javascript implementation?

The docs don’t mention anything specific.
http://developer.playcanvas.com/en/api/pc.math.html#random

At first I suspected it was performance related, but after doing some rudimentary tests (below) I’m not so sure…

defaultRandom: 436ms
playCanvasRandom: 528ms

Perhaps there is something significant about the pc namespace that I haven’t picked up yet?

    var testSize = 10000000
    // test random
    console.time('defaultRandom');
    defaultRandom(testSize);
    console.timeEnd('defaultRandom');
    
    console.time('playCanvasRandom');
    playCanvasRandom(testSize);
    console.timeEnd('playCanvasRandom');
    
    
    function defaultRandom(testSize) {
        var a1 = [];
        while(a1.length < testSize) {
            a1.push(Math.random(1,testSize));
        }
    }
    
    function playCanvasRandom(testSize) {
        var a1 = [];
        while(a1.length < testSize) {
            a1.push(pc.math.random(1,testSize));
        }
    }

The PlayCanvas pc.math.random() is just a wrap around to allow use of min and max bounds as Math.random only returns a value between 0 and 1.

From GitHub:

    /**
     * @function
     * @name pc.math.random
     * @description Return a pseudo-random number between min and max.
     * The number generated is in the range [min, max), that is inclusive of the minimum but exclusive of the maximum.
     * @param {Number} min Lower bound for range.
     * @param {Number} max Upper bound for range.
     * @returns {Number} Pseudo-random number between the supplied range.
     */
    random: function (min, max) {
        var diff = max - min;
        return Math.random() * diff + min;
    },
3 Likes