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

pure-random

v0.1.1

Published

A purely functional random number generator

Downloads

28

Readme

pure-random

Build Status

A purely functional random number generator. It implements the xorshift algorithm. Xorshift RNGs which are a class of PRNGs that are extremely fast on modern computers and outperm other PRNGs in performance and BigCrush stress testing.

Usage

> const rnd = require('pure-random')
> rnd.genCsSeed()
[ 2628481196, 1837393298, 2892949381, 1706851469 ]
> rnd.random(rnd.genCsSeed(), 0, 10)
Right(8)

Passing in the same seed with the same params produces the same number:

> const seed = rnd.genCsSeed()
> seed
[ 426141121, 700962946, 3633913687, 2605998810 ]
> rnd.random(seed, 0, 10)
Right(7)
> rnd.random(seed, 0, 10)
Right(7)
> rnd.random(seed, 0, 10)
Right(7)

Reference

genSeed

:: () -> [Uint32]

Uses Date.now to generate a non-cryptographically secure seed. It is more performant than genCsSeed, and appropriate for most cases. The seed is an array of four Uint32 numbers. Since there is no Uint32 in javascript yet, integers within the range [0, 4294967295] are returned.

genCsSeed

:: () -> [Uint32]

Uses crypto.randomBytes to generate a cryptographically secure seed. It is useful for creating an initialization vector for cryptography, or in any situation where you need the generated random number to have significant entropy. The seed is an array of four Uint32 numbers. Since there is no Uint32 in javascript yet, integers within the range [0, 4294967295] are returned.

randUint

:: [UInt32] -> UInt32

Takes a seed and returns a random UInt32 value, which in javascript is an integer within the range [0, 4294967295]. This function is actually the javascript xorshift implementation, so it is extremely fast. The other methods call this method under the hood.

randFloat

 :: [UInt32] -> Int -> Int -> Either Error Float

Takes a seed and a min and max range. It returns an Either value that is Left Error if the function was called with invalid parameters, or a Right Float within the specified range (inclusive).

random

 :: [UInt32] -> Int -> Int -> Either Error Int

Takes a seed and a min and max range. It returns an Either value that is Left Error if the function was called with invalid parameters, or a Right Int within the specified range (inclusive).

Faq

Why wasn't Math.random used?

Math.random is non-deterministic and doesn't have take a seed value, which means that you cannot return results consistently, which is against the purely functional paradigm. Note that this library has an option for more powerful entropy than Math.random by using the genCsSeed option.

What about xorshift* or xorshift+?

The javascript number type uses a double-precision 64-bit binary format IEEE 754 value, which is a number between -(253 - 1) and 253 - 1. The xorshift* and xorshift+ libraries rely on Uint64 types, which javascript does not support. If a developer erroneously decides to represent Uint64 using the native number type, then any Uint64 type will lose precision on the upper end because a javascript number will only go up to 253 - 1 but a range of 0 to 264 - 1 is needed for Uint64.

In general, it would be too complicated to implement these other algorithms because you would have to jump through a lot of hoops with javascript's lack of a type system, and with very little gain. These improvements to xorshift are only slightly more performant.