Comments (8)
I agree that Math.random()
isn't good enough and that a seeded random is extremely useful. I will take a look on the document and implementations and check out if we can use it or not.
from amd-utils.
Do you think Mersenne Twister would do the trick? I used that once to get repeatable random numbers (you can set the seed). Perhaps it will work for your purposes too.
from amd-utils.
The page I referenced mentions Mersenne Twister a couple times and precludes using it since it fails Crush randomness tests.
from amd-utils.
I was thinking about it and I fell that providing a single algorithm is not going to be a good idea and is also out of the scope of this project (besides the fact that it is really hard to test PRNG properly).
I think we should add a way to swap the PRNG used by each method so you could curry each method individually and use different seeds for each one:
var mt = new MersenneTwister('magic beans seed');
var mtRandom = function(){ return mt.random() };
var randInt_1 = randInt.create( mtRandom );
var randInt_2 = randInt.create( KISS07('sunflower seed') );
And also add a way to replace it on an existing instance:
// default value = Math.random;
randInt.random = Kybos('foobar');
And maybe create a new module random/random
that is used by all the methods inside the random
package (avoid calling Math.random()
directly), so replacing it would be enough to propagate the change to all random methods:
define(function(){
function random(){
return random.random();
}
// expose a `random` property so user can swap the behavior dynamically
random.random = Math.random;
return random;
});
Could also use the RequireJS map config to replace the amd-utils/random/random
module, but I like the idea of exposing the random
property since on node.js there is no such a thing as a map
config.
PS: that way we also avoid any licensing issues.
from amd-utils.
I definitely like the flexibility that having a separate module, random/random
, provides. I assume the default implementation will still be Math.random()
. There should probably be some comment about the unreliability of Math.random()
especially for UUID generation.
Having said that random/random
looks a little funny. Maybe random/gen
? random/generator
? random/ng
? random/prng
?
from amd-utils.
I added the random/random
, since I think it will only be used internally (easier to call Math.random
directly) I think the name is good enough.
I didn't implemented the randInt.create()
, choice.create()
, etc.. or added a new property random
to each method since I think it will increase complexity a lot and the global replacement should be enough for most use cases (overriding amd-utils/random/random
behavior).
I improved the documentation a lot adding info on the top about Math.random
usage and also improved the guid
documentation to mention safety problems. random/random
gets into a lot of details so it should be clear to everyone: http://millermedeiros.github.com/amd-utils/random.html
Thanks for all the feedback and for helping me to improve amd-utils. Feel free to reopen the issue.
from amd-utils.
for future reference this article is great: http://www.empiricalzeal.com/2012/12/21/what-does-randomness-look-like/
from amd-utils.
Thanks! I'll check it out.
from amd-utils.
Related Issues (20)
- use npm prepublish script instead of install
- some users need the AMD modules even if using npm install HOT 3
- add math/round - similar to math/snap but round value instead of floor
- rename math/snap to math/floor?
- lang/isNaN doesn't catch `new Number(NaN)`
- Create tag 0.9.0 HOT 1
- Add deepFilIIn HOT 3
- Ability to test functions directly in the documentation HOT 2
- integrate travis-ci
- make unit tests work on node.js
- integrate some kind of code coverage test
- rename project? HOT 11
- split into multiple projects/packages? HOT 8
- Remove node_modules from git HOT 7
- deprecate financial package? HOT 1
- clone function does not behave well on instances HOT 6
- support exiting iterations early if callback return `false`
- Function currying HOT 4
- Defer to native methods HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from amd-utils.