npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bluelovers/xorshift

v1.2.1

Published

Random number generator using xorshift128+

Downloads

19

Readme

xorshift Build Status

Pseudorandom number generator using xorshift128+

Installation

npm install xorshift

Example

var xorshift = require('xorshift').default;

for (var i = 0; i < 10; i++) {
  console.log(xorshift.random()); // number in range [0, 1)
}

Documentation

This module exports a default pseudo random generator. This generators seed have already been set (using Date.now()). If this is not suitable a custom generator can be initialized using the constructor function xorshift.constructor. In both cases random numbers can be generated using the two methods .random and .randomint.

var xorshift = require('xorshift').default;

xorshift.random()

This method returns a random 64-bit double, with its value in the range [0, 1). That means 0 is inclusive and 1 is exclusive. This is equivalent to Math.random().

console.log(xorshift.random()); // number between 0 and 1

This method will serve most purposes, for instance to randomly select between 2, 3 and 4, this function can be used:

function uniformint(a, b) {
  return Math.floor(a + xorshift().random() * (b - a));
}

console.log(uniformint(2, 4));

xorshift.randomint()

This method returns a random 64-bit integer. Since JavaScript doesn't support 64-bit integers, the number is represented as an array with two elements in big-endian order.

This method is useful if high precision is required, the xorshift.random() method won't allow you to get this precision since a 64-bit IEEE754 double only contains the 52 most significant bits.

var bview = require('binary-view');
console.log(bview( new Uint32Array(xorshift.randomint()) ));

xorshift.constructor

This method is used to construct a new random generator, with a specific seed. This is useful when testing software where random numbers are involved and getting consistent results is important.

var XorShift = require('xorshift').default.constructor;
var rng1 = new XorShift([1, 0, 2, 0]);
var rng2 = new XorShift([1, 0, 2, 0]);

assert(rng1.random() === rng2.random());

A XorShift instance have both methods random and randomint. In fact the xorshift module is an instance of the XorShift constructor.

The constructor can also be accessed as require('xorshift').XorShift, which is useful when using the import syntax.

import { XorShift } from 'xorshift'

Reference

This module implements the xorshift128+ pseudo random number generator.

This is the fastest generator passing BigCrush without systematic errors, but due to the relatively short period it is acceptable only for applications with a very mild amount of parallelism; otherwise, use a xorshift1024* generator. – http://xorshift.di.unimi.it

This source also has a reference implementation for the xorshift128+ generator. A wrapper around this implementation has been created and is used for testing this module. To compile and run it:

gcc -O2 reference.c -o reference
./reference <numbers> <seed0> <seed1>
  • <numbers> can be any number greater than zero, and it will be the number of random numbers written to stdout. The default value is 10.
  • <seed0> and <seed1> forms the 128bit seed that the algorithm uses. Default is [1, 2].

License

This software is licensed under "MIT"

Copyright (c) 2014 Andreas Madsen & Emil Bay

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.