<repo_name>Oipo/xoroshirojs128plus<file_sep>/README.md
xoroshirojs is a nodejs addon for http://xorshift.di.unimi.it/xoroshiro128plus.c. For more information see http://xorshift.di.unimi.it/. It's currently only tested on linux.

# Usage

`npm i --save xoroshirojs128plus`

```javascript
var xoroshirojs = require('xoroshirojs128plus')

var ret = xoroshirojs.seed(10, 11)

for (var i = 0; i < 5; i++) {
    ret = xoroshirojs.next()
    console.log(ret)
}
```

To seed the PRNG, we need a 64 bit unsigned integer. JS does not have a safe native 64 bit unsigned integer so instead we give 2 32 bit unsigned integers `xoroshirojs.seed(10, 11)`. Once seeded, use `val result = xoroshirojs.next()` to get an array of 2 32 bit unsigned integers since, again, xoroshirojs returns a 64 bit value.

# Thanks

Thanks to <NAME> for splitmix64
Thanks to <NAME> and <NAME> for xoroshiro128plus
<file_sep>/binding.gyp
{
  "targets": [
    {
      "target_name": "xoroshirojs",
      "sources": [ "xoroshirojs.cc", "splitmix64.cc", "xoroshiro128plus.cc" ],
      "cflags": ["-fno-move-loop-invariants", "-fno-unroll-loops"],
      "include_dirs": [
        "<!(node -e \"require('nan')\")"
      ]
    }
  ]
}
<file_sep>/xoroshiro128plus.cc
/*  Written in 2016 by <NAME> and <NAME> (<EMAIL>)

To the extent possible under law, the author has dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.

See <http://creativecommons.org/publicdomain/zero/1.0/>. */

#include "xoroshiro128plus.h"

/* This is the successor to xorshift128+. It is the fastest full-period
   generator passing BigCrush without systematic failures, but due to the
   relatively short period it is acceptable only for applications with a
   mild amount of parallelism; otherwise, use a xorshift1024* generator.

   Beside passing BigCrush, this generator passes the PractRand test suite
   up to (and included) 16TB, with the exception of binary rank tests,
   which fail due to the lowest bit being an LFSR; all other bits pass all
   tests.

   Note that the generator uses a simulated rotate operation, which most C
   compilers will turn into a single instruction. In Java, you can use
   Long.rotateLeft(). In languages that do not make low-level rotation
   instructions accessible xorshift128+ could be faster.

   The state must be seeded so that it is not everywhere zero. If you have
   a 64-bit seed, we suggest to seed a splitmix64 generator and use its
   output to fill s. */

uint64_t s[2];

static inline uint64_t rotl(const uint64_t x, int k) {
	return (x << k) | (x >> (64 - k));
}

void xoroshiro_seed(uint64_t high, uint64_t low) {
	s[0] = high;
	s[1] = low;
}

uint64_t xoroshiro_next(void) {
	const uint64_t s0 = s[0];
	uint64_t s1 = s[1];
	const uint64_t result = s0 + s1;

	s1 ^= s0;
	s[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14); // a, b
	s[1] = rotl(s1, 36); // c

	return result;
}
<file_sep>/package.json
{
  "name": "xoroshirojs128plus",
  "version": "0.0.3",
  "description": "Node.js version of the PRNG xoroshiro128plus",
  "main": "xoroshiro.js",
  "private": false,
  "homepage": "https://github.com/Oipo/xoroshirojs128plus",
  "license": "MIT",
  "dependencies": {
    "bindings": "latest",
    "nan": "latest"
  },
  "scripts": {
    "test": "node test/xoroshiro.js",
    "install": "node-gyp rebuild"
  },
  "gypfile": true,
  "devDependencies": {
    "chai": "^3.5.0"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/Oipo/xoroshirojs128plus.git"
  },
  "author": "<NAME> (https://github.com/Oipo)",
  "bugs": {
    "url": "https://github.com/Oipo/xoroshirojs128plus/issues"
  }
}
<file_sep>/xoroshiro128plus.h
#include <stdint.h>

extern "C" {
    void xoroshiro_seed(uint64_t high, uint64_t low);
    uint64_t xoroshiro_next(void);
}
<file_sep>/xoroshiro.js
var xoroshirojsBinding = require('bindings')('xoroshirojs.node')

exports.seed = function (high, low) {
    return xoroshirojsBinding.seed(high, low);
}

exports.seedWithCurrentTime = function () {
    var ms = (new Date).getTime();
    return xoroshirojsBinding.seed(ms%1000, ms);
}

exports.next = function () {
    return xoroshirojsBinding.next();
}
<file_sep>/test/xoroshiro.js
var xoroshirojs = require('../xoroshiro')
var chai = require('chai');

chai.expect(xoroshirojs).to.not.be.undefined;

chai.expect(xoroshirojs.seed).to.throw("Wrong arguments, expected number");
chai.expect(xoroshirojs.next).to.throw("Call Seed first before requesting a value");

var ret = xoroshirojs.seed(10, 11)

chai.expect(ret).to.equal(true)

for (var i = 0; i < 5; i++) {
    ret = xoroshirojs.next()
    console.log(ret)
}
<file_sep>/splitmix64.h
#include <stdint.h>

extern "C" {
    uint64_t splitmix64_next(uint64_t x);
}
<file_sep>/xoroshirojs.cc
#include <nan.h>
#include "splitmix64.h"
#include "xoroshiro128plus.h"

bool seeded = false;

NAN_METHOD(Seed) {
  if (info.Length() != 2) {
    Nan::ThrowTypeError("Wrong number of arguments, expected 2");
    return;
  }

  if (!info[0]->IsNumber()) {
    Nan::ThrowTypeError("Wrong arguments, expected number");
    return;
  }

  if (!info[1]->IsNumber()) {
    Nan::ThrowTypeError("Wrong arguments, expected number");
    return;
  }

  uint64_t arg0 = info[0]->Uint32Value();
  uint64_t arg1 = info[1]->Uint32Value();
  uint64_t seed = (arg0 << 32) + arg1;
  uint64_t seed2 = splitmix64_next(seed);

  xoroshiro_seed(seed, seed2);
  seeded = true;

  info.GetReturnValue().Set(true);
}

NAN_METHOD(Next) {
    if (info.Length() != 0) {
        Nan::ThrowTypeError("Wrong number of arguments, expected 0");
        return;
    }

    if (!seeded) {
        Nan::ThrowTypeError("Call Seed first before requesting a value");
        return;
    }

    uint64_t val = xoroshiro_next();
    v8::Local<v8::Array> result = Nan::New<v8::Array>(2);
    Nan::Set(result, 0, Nan::New<v8::Number>((uint32_t)(val >> 32)));
    Nan::Set(result, 1, Nan::New<v8::Number>((uint32_t)val));

    info.GetReturnValue().Set(result);
}

void Init(v8::Local<v8::Object> exports) {
    exports->Set(Nan::New("seed").ToLocalChecked(),
        Nan::New<v8::FunctionTemplate>(Seed)->GetFunction());
    exports->Set(Nan::New("next").ToLocalChecked(),
        Nan::New<v8::FunctionTemplate>(Next)->GetFunction());
}

NODE_MODULE(xoroshiro, Init)
