-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rand() implementation not really random #542
Comments
Yup, turns out it's hard to do random number generation properly... we were doing it wrong. There are some decent ways to do it in 32 bits, but they all run into JS numeric limits since they end up doing 64-bit math. So I switched it to Math.random like you suggest. (Note though that it needs to be Downside of this is we ignore the seed now, so results are not reproducible. But I think it's the right tradeoff. Thanks for filing! |
Hi @kripken, I hear Thanks |
Actually, I made a mistake. Didn't realize that Thanks! Reference:
|
Sorry to spam but I think an initialization parameter to toggle present behavior to a throw could work.. or even a fallback (callback) function (for the purpose of displaying alerts etc) which can preferentially return undef/null/zero random value, which would result in a throw |
I see what you mean here. However, I think in JS you just need to add 1 line, like
Which is easy to add for those that need it, so I think adding an emscripten option is overkill. |
That's an interesting point. Nice solution. Thanks! |
@kripken now that I've finally gotten around to actually implementing this, I realize I have a small handful of trivial usages of Math.random that it would be fairly awkward to work around. I have settled on making a 'random' interface fn and having all of them implement it. Not quite optimal. |
@kripken Turns out our other users are blocked by this too, and the solution of throwing does not work for them. I would like to request that we move the |
Where in emscripten do you need to call your custom random function? If it's in a function in the JS library, then you can just override it with another JS library, for example. |
@kripken No, it's not that… there's a |
I see. Yeah, I guess it makes sense to throw there - a PR with that sounds good. |
This now throws on modern browsers for me. Is the crypto feature detection In library_fs.js correct? It looks for |
This regressed, but was fixed in fb03abc (in 1.38.27). |
rand() implementation from library.js exhibits an odd behaviour.
Compiling with emcc and executing with "node a.out.js" outputs:
$ node a.out.js
999 1 0 0 0 0 0 0
$ node a.out.js
996 4 0 0 0 0 0 0
$ node a.out.js
999 1 0 0 0 0 0 0
Replacing rand() implementation by
produces the correct result.
The text was updated successfully, but these errors were encountered: