basiljs/basil.js

random is broken

clankill3r opened this issue · 4 comments

The values produced by random are like 1000 times lower then they should be.

For example, this are the numbers between 4 and 8:

4.00001660175622
4.00000739656389
4.00002503395081
4.00005162227899
4.00000867806375
4.00003036111593
4.00005471613258
4.00003544334322
4.00005575269461
4.00001819804311

Hi @clankill3r Thanks for the bug report. Are you referring to the v2 version or are you using v1?
This is the code we should be looking at

var currentRandom = Math.random;
/**
* Generates random numbers. Each time the random() function is called, it returns an unexpected value within the specified range. If one parameter is passed to the function it will return a float between zero and the value of the high parameter. The function call random(5) returns values between 0 and 5. If two parameters are passed, it will return a float with a value between the the parameters. The function call random(-5, 10.2) returns values between -5 and 10.2.
*
* One parameter sets the range from 0 to the given parameter, while with two parameters present you set the range from val1 - val2.
*
* @cat Math
* @subcat Random
* @method random
* @param {Number} [low] The low border of the range
* @param {Number} [high] The high border of the range
* @return {Number} A random number
*/
pub.random = function() {
if (arguments.length === 0) return currentRandom();
if (arguments.length === 1) return currentRandom() * arguments[0];
var aMin = arguments[0],
aMax = arguments[1];
return currentRandom() * (aMax - aMin) + aMin;
};

For comparison here is the code from p5js https://github.com/processing/p5.js/blob/c3f4121619174db5eaaf09b8ca51c93d530caa27/src/math/random.js#L86-L149

Can you provide the code you used to create these values? Or even better write a failing test based on the v2 branch?

Ok, I did some tests. As soon as you use a randomSeed it breaks.

#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basiljs/bundle2/basil.js";


function draw() {

    clear(page());

    randomSeed(43535);

    txt = "";

    for (var i = 0; i < 25; i++) {
        txt += random(4, 8) + "\n"; 
    }

    
    textSize(100);
    text(txt, 50, 50, 4096, 4096);
}

Thank you @clankill3r We will have a look ASAP.

ffd8 commented

Heyhey - just ran into this with a student = randomSeed() and noiseSeed() both seem to be broken, as they are producing the same results regardless of usage... I'll also have a look soon, unless something was found last year?