Comparing version 0.0.1 to 0.0.2
15
alea.js
@@ -49,2 +49,11 @@ // From http://baagoe.com/en/RandomMusings/javascript/ | ||
}; | ||
random.export = function(){ | ||
return [s0, s1, s2, c] | ||
}; | ||
random.import = function(i){ | ||
s0 = +i[0] || 0; | ||
s1 = +i[1] || 0; | ||
s2 = +i[2] || 0; | ||
c = +i[3] || 0; | ||
}; | ||
random.version = 'Alea 0.9'; | ||
@@ -57,2 +66,8 @@ random.args = args; | ||
Alea.import = function(i){ | ||
var random = new Alea(); | ||
random.import(i); | ||
return random; | ||
}; | ||
function Mash() { | ||
@@ -59,0 +74,0 @@ var n = 0xefc8249d; |
{ | ||
"name": "alea", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Implementation of the Alea PRNG by Johannes Baagøe", | ||
@@ -9,5 +9,12 @@ "main": "alea.js", | ||
}, | ||
"repository": "https://github.com/coverslide/node-alea", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/coverslide/node-alea" | ||
}, | ||
"author": "", | ||
"license": "BSD" | ||
"license": "BSD", | ||
"readme": "# Alea\n\nA simple copy-and-paste implementation of Johannes Baagøe's Alea PRNG\n\nMostly packaged so I can easily include it in my projeccts. Nothing more\n\n## Installation\n\n\tnpm install alea\n\n## Usage\n\n\tvar Alea = require('alea')\n\t\n\tvar prng = new Alea() // add an optional seed param\n\n\tvar nextRandnum = prng() // just call the return value of Alea\n\n## Acknowledgements\n\nEverything in this module was made by Johannes Baagøe. I just wanted this in npm.\nRead more on his [homepage](http://baagoe.org/).\n", | ||
"readmeFilename": "README.md", | ||
"_id": "alea@0.0.1", | ||
"_from": "alea" | ||
} |
@@ -7,2 +7,4 @@ # Alea | ||
JavaScript's Math.random() is fast, but has problems. First, it isn't seedable, second, its randomness leaves a bit to be desired. The Mersenne Twister algorithm is a popular algorithm to replace random implemenetations, and has even been ported to JavaScript. Unfortunately, it depends on bitwise operations which can be slow in JavaScript. [Johannes Baagøe](http://baagoe.org/) has done some great work in trying to find a more modern PRNG algorithm that performs well on JavaScript, and Alea seems to be the one that has come out ahead ([benchmarks](http://jsperf.com/prng-comparison)). | ||
## Installation | ||
@@ -20,2 +22,20 @@ | ||
## Additions | ||
Also adds the ability to sync up two Alea PRNGs via the import and export methods. While you can initialize two Alea PRNGs with the same seed, you cannot sync up a new PRNG after an old PRNG has already started running. This is useful for games where a new player joins and their local PRNG should sync up with the remote one. | ||
var prng1 = new Alea(200) | ||
prng1() | ||
prng1() | ||
// after generating a few random numbers, we will initialize a new PRNG | ||
var prng2 = Alea.import(prng1.export()) | ||
// this should echo true, true, true | ||
console.log(prng2() == prng1()) | ||
console.log(prng2() == prng1()) | ||
console.log(prng2() == prng1()) | ||
## Acknowledgements | ||
@@ -22,0 +42,0 @@ |
18
test.js
@@ -16,2 +16,20 @@ var assert = require('assert') | ||
prng1() | ||
prng1() | ||
prng1() | ||
var e = prng1.export() | ||
var prng4 = Alea.import(e) | ||
assert.equal(prng1(), prng4()) | ||
assert.equal(prng1(), prng4()) | ||
assert.equal(prng1(), prng4()) | ||
prng2.import(prng1.export()) | ||
assert.equal(prng1(), prng2()) | ||
assert.equal(prng1(), prng2()) | ||
assert.equal(prng1(), prng2()) | ||
console.log("All tests Passed!") |
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
5139
105
43
0