Skip to content

Math.random in mode:gpu #498

Closed
Closed

Description

@marcelloexp386

A GIF or MEME to give some spice of the internet

What is wrong?

The random number distribution is Normal instead of Uniform[0,1]

Where does it happen?

Ex. In GPU.js when I run Math.random inside a gpu.createKernel(function() in Node.js on my pc windows 10 and Nvidia GPU
NB If mode is set to cpu, all else equal, the numbers are Unifom

How do we replicate the issue?

const { GPU } = require('gpu.js');
const gpu = new GPU({ mode: 'gpu' });
const nobs=10000;

const kernel = gpu.createKernel(function() {
const y=Math.random();
return y;
}, { output: [nobs] });
const data = kernel();

How important is this (1-5)?

5

Expected behavior (i.e. solution)

the data should be uniform

Other Comments

Probably, in the plugin you should have chosen nrandom instead of n4random.

Activity

  1. robertleeplummerjr commented on Sep 18, 2019

    @robertleeplummerjr
    Member

    Do you have a link to that function? It was super difficult to find any insight when implementing.

  2. marcelloexp386 commented on Sep 18, 2019

    @marcelloexp386
    Author

    Hi Robert, I am quite new to gpu.js and zero-knowledge of WebGL, shaders ... so I think to be quite hopeless in finding a solution. To be totally honest I just had the intuition that the problem was in the choice of n4rand.

    In https://www.shadertoy.com/view/4t2SDh, you can find the file generating the noise with the 4 different functions. One can see that nrand produce a uniform histogram, while the others n1rand, n2rand, n3rand and n4 rand produces triangle and bell shaped histograms.

    In your plugin triangle-noise.js, you make a functionReplace of Math.random() with n4rand(vTexCoord). If I interpreted correctly your code, the right function should be nrand.

    My script for testing the statistical properties of the random numbers is attached here (as I said, I run it with node and one can alternatively select "cpu" or "gpu" in line 10). If you take the data saved in the txt file and plot the histogram you will immediately see the bell shaped curve for the data generated with the "gpu" mode.
    stampa_randnumbers_GPU_v01.js.txt

  3. robertleeplummerjr commented on Sep 18, 2019

    @robertleeplummerjr
    Member

    Fantastic! Ty for not attacking this, it was quite the thing to figure out. I'll review asap, and get it corrected. Again TY!

  4. marcelloexp386 commented on Sep 18, 2019

    @marcelloexp386
    Author

    Thanks to you and the gpu team for the fantastic job you are doing with gpu.js!

  5. robertleeplummerjr commented on Sep 18, 2019

    @robertleeplummerjr
    Member

    ty for trying it!

  6. marcelloexp386 commented on Sep 20, 2019

    @marcelloexp386
    Author
  7. marcelloexp386 commented on Oct 16, 2019

    @marcelloexp386
    Author
  8. robertleeplummerjr commented on Oct 18, 2019

    @robertleeplummerjr
    Member

    Hi Robert, did you succeed in changing algorithm?>

    I tried some changes out this morning on this branch: https://github.com/gpujs/gpu.js/tree/498-uniformly-distributed
    Can you take look and see if that is good enough for your needs?
    I can see a noticeable improvement when comparing them on the ./examples/random.html page.

  9. robertleeplummerjr commented on Oct 29, 2019

    @robertleeplummerjr
    Member

    FYI: https://github.com/gpujs/gpu.js/tree/498-uniformly-distributed will be merged soon, and addresses the issue brought up here.

  10. marcelloexp386 commented on Oct 29, 2019

    @marcelloexp386
    Author

    Hi Robert, tremendous improvement! I run a "pi" test (i.e. estimation of the pi value) and now the results do not show anymore the systematic bias. Still, the GPU method produces worse estimates (in the sense of higher stdev) than the CPU method, but this was expected. If, somewhere in your blog, you can mention RandomPower for having contributed to your project, it would be very much appreciated. RandomPower is a university spin-off where we use a quantistic effect of the light in order to generate True Random Numbers .

  11. robertleeplummerjr commented on Oct 29, 2019

    @robertleeplummerjr
    Member

    Do you have a link to RandomPower?

  12. marcelloexp386 commented on Oct 29, 2019

    @marcelloexp386
    Author
  13. robertleeplummerjr commented on Oct 29, 2019

    @robertleeplummerjr
    Member

    Can you get me more details on the contribution?

  14. marcelloexp386 commented on Oct 29, 2019

    @marcelloexp386
    Author

    ? In reality, my contribution was just about "testing" your random number generator algorithm. So, I would simply mention randompower.eu among the contributors to the gpujs project.

  15. robertleeplummerjr commented on Oct 29, 2019

    @robertleeplummerjr
    Member

    Awesome! Is there any sort of qualification that our random generates that we can make mention of?

  16. marcelloexp386 commented on Oct 29, 2019

    @marcelloexp386
    Author

    We can analyze their statistical properties but it will take a bit of time because at the moment we are working on the hardware of our project. As soon as we have a couple of days free, we can come back with some stats.
    If/when you put a link to our website somewhere in your blog, let me know.

  17. robertleeplummerjr commented on Oct 29, 2019

    @robertleeplummerjr
    Member

    I'll just put a shout out to RandomPower in the readme if that is ok. That way everyone sees it. Also, I very much look forward to those test results!

  18. robertleeplummerjr commented on Oct 29, 2019

    @robertleeplummerjr
    Member
  19. marcelloexp386 commented on Oct 29, 2019

    @marcelloexp386
    Author
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions