Crates.io | park-miller-carta-prng |
lib.rs | park-miller-carta-prng |
version | 0.1.3 |
source | src |
created_at | 2018-02-01 14:30:57.774059 |
updated_at | 2018-02-06 19:16:52.512682 |
description | An efficient Pseudo-Random Number Generator |
homepage | |
repository | https://github.com/kenOfYugen/park-miller-carta-prng |
max_upload_size | |
id | 49153 |
size | 107,356 |
(documentation in progress) This is a multi-language repository, demonstrating C-Rust-Node.js-Web interoperability/portability.
It utilizes Rust as a build system, in order to compile the original C implemetations by Robin Whittle. A faithful Rust port is also included, cross-compilable to asm.js and WebAssembly via Emscripten, for use in Node.js and browsers.
Add with npm i --save --only=production park-miller-carta-prng
.
index.html:
<!DOCTYPE html>
<html>
<head>
<title>Wasm</title>
</head>
<body>
<script type="text/javascript">
var Module = {};
var prng = {
seed: (i) => Module.prng_new(i),
destroy: (ptr) => Module.prng_destroy(ptr),
getInteger: (ptr) => Module.next_unsigned_integer(ptr),
getFloat: (ptr) => Module.next_unsigned_float(ptr)
};
function fetchAndInstantiate(url, importObject) {
return fetch(url).then(response =>
response.arrayBuffer()
).then(bytes =>
WebAssembly.instantiate(bytes, importObject)
).then(results =>
results.instance
);
}
fetchAndInstantiate('node_modules/park-miller-carta-prng/dist/wasm/browser-standalone.wasm', {})
.then( mod => {
Module.prng_new = mod.exports.prng_new;
Module.prng_destroy = mod.exports.prng_destroy;
Module.next_unsigned_integer = mod.exports.next_unsigned_integer;
Module.next_unsigned_float = mod.exports.next_unsigned_float;
})
.then( () => {
var ptr = prng.seed(1);
var container = document.createElement('div');
container.textContent = `integer: ${prng.getInteger(ptr)} float: ${prng.getFloat(ptr)}`
document.body.appendChild(container);
prng.destroy(ptr);
});
</script>
</body>
</html>
host the html via python2 -m SimpleHTTPServer
/python3 -m http.server
of your preferred method.
Add with npm i --save --only=production park-miller-carta-prng
.
index.js:
let {prng, asmjs, wasm} = require("park-miller-carta-prng");
let assert = require('assert');
// use asm.js version
let generator = prng(asmjs)(1);
assert(generator.getInteger() === 16807);
generator.destroy();
// use wasm version
generator = prng(wasm)(1);
assert(generator.getInteger() === 16807);
generator.destroy();
You should always call destroy()
when you are done using the generator.
Available methods:
prng
, takes either the supplied asmjs
, or wasm
modules, generated by emcc
.
Then, it must be supplied with a positive seed integer.getInteger
, getFloat
and destroy
methods, are self explanatory.Clone the repository and npm run build:addon
.
check src/test.coffee
for usage.
[Todo] simple API, conditional build on npm download.
Add park-miller-carta-prng
to [dependencies]
in Cargo.toml
main.rs:
extern crate prng;
use prng::PRNG;
fn main() {
let mut prng = PRNG::new(1);
let random_float = prng.next_unsigned_float();
assert_eq!(0.000007826369, random_float);
let random_int = prng.next_unsigned_integer();
assert_eq!(282475249, random_int);
}
git clone https://github.com/kenOfYugen/park-miller-carta-prng
cd park-miller-carta-prng
--release
flag for optimized builds)cargo b
cargo b --target asmjs-unknown-emscripten
cargo b --target wasm32-unknown-emscripten
cargo b --target wasm32-unknown-unknown
npm i && npm run build:all
emscripten
emscripten
--target wasm32-unknown-unknown