Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

random is broken #371

Open
clankill3r opened this issue May 24, 2022 · 4 comments
Open

random is broken #371

clankill3r opened this issue May 24, 2022 · 4 comments

Comments

@clankill3r
Copy link

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
@ff6347
Copy link
Member

ff6347 commented May 25, 2022

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?

@clankill3r
Copy link
Author

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);
}

@ff6347
Copy link
Member

ff6347 commented May 30, 2022

Thank you @clankill3r We will have a look ASAP.

@ffd8
Copy link
Member

ffd8 commented Apr 26, 2023

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?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants