xoroshiro128starstar
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

xoroshiro128starstar

A WebAssembly port of xoroshiro128starstar.c using AssemblyScript.

Uses 2 u64 globals for the state so the WebAssembly module does not allocate any pages of memory and is only 262 bytes.

Install

npm install xoroshiro128starstar

Usage

Using with random

const random = require("random");
const SEED = 12345;
random.use(require("xoroshiro128starstar")(SEED));
 
// log normal distributed random variable
// mu = 0 and sigma = 1
random.logNormal(0, 1);

Seeded Math.random replacement

const SEED = 12345;
const Math.random = require("xoroshiro128starstar")(SEED);
 
function randomInt(start, end) {
  return (Math.random() * (end - start + 1) + start) | 0;
}
 
// returns a uniform random between 0 and 100 inclusive.
randomInt(0, 100);

Benchmark

bench.js

➜  xoroshiro128starstar git:(master) ✗ node --version
v10.3.0
➜  xoroshiro128starstar git:(master) ✗ node --expose-gc bench.js
testing
- wasmrandom (<= int < 10)
- mathrandom (<= int < 10)
- seedrandom (<= int < 10)
running first test, please wait...
frequency
'0: 41069751',
  '1: 41073989',
  '2: 41078799',
  '3: 41070726',
  '4: 41072933',
  '5: 41084158',
  '6: 41074424',
  '7: 41084506',
  '8: 41084073',
  '9: 41083843' ]
  wasmrandom (<= int < 10) ... 81,321,598.18 op/s
frequency
'0: 37063747',
  '1: 37079010',
  '2: 37069394',
  '3: 37063328',
  '4: 37062678',
  '5: 37069730',
  '6: 37068098',
  '7: 37068668',
  '8: 37066801',
  '9: 37058401' ]
  mathrandom (<= int < 10) ... 73,658,101.35 op/s
frequency
'0: 11982125',
  '1: 11982173',
  '2: 11981276',
  '3: 11980644',
  '4: 11984042',
  '5: 11983492',
  '6: 11986362',
  '7: 11977713',
  '8: 11986711',
  '9: 11982949' ]
  seedrandom (<= int < 10) ... 24,057,933.98 op/s
fastest: wasmrandom (<= int < 10)

Package Sidebar

Install

npm i xoroshiro128starstar

Weekly Downloads

4

Version

1.1.1

License

BSD-2-Clause

Unpacked Size

13.7 kB

Total Files

9

Last publish

Collaborators

  • krisselden