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

@herbcaudill/random

v1.0.5

Published

Utility random functions. Uses `random-seed` so the randomizer can be seeded if you need determinism (e.g. for testing).

Downloads

867

Readme

Utility random functions. Uses random-seed so the randomizer can be seeded if you need determinism (e.g. for testing).

Installation

yarn add @herbcaudill/random

Usage

import { makeRandom } from '@herbcaudill/random'

// if you provide a seed, you will get the same sequence of random numbers each time
const r = makeRandom('12345')

// if you don't provide a seed, Math.random() will be used to generate a seed; so you'll get a different sequence every time.
const r = makeRandom()

r.integer(min = 0, max = 10)

Returns an integer greater than or equal to min, and less than max, with uniform distribution.

const a = r.integer() // returns a number between 0 and 10 (could be 0, could be 9, will never be 10)
// a = 7

const b = r.integer(1, 5) // returns a number between 1 and 5 (could be 1, could be 4, will never be 5)
// b = 4

r.decimal(min = 0, max = 1)

Returns an decimal number greater than min, and less than max, with uniform distribution.

const a = r.decimal() // returns a number between 0 and 1 (greater than 0, less than 1)
// a = 0.389867019774243

const b = r.decimal(1, 5) // returns a number between 1 and 5 (greater than 1, less than 5)
// b = 4.217256259412135

const values = range(100000).map(_ => r.decimal(0, 10))

// 0 -  1: 9889
// 1 -  2: 10030
// 2 -  3: 9947
// 3 -  4: 10042
// 4 -  5: 10009
// 5 -  6: 9902
// 6 -  7: 10059
// 7 -  8: 10065
// 8 -  9: 10108
// 9 - 10: 9949

r.normal(min = 0, max = 1)

Returns a decimal number greater than min, and less than max, with normal distribution.

const a = r.normal() // returns a number between 0 and 1 (greater than 0, less than 1)
// a = 0.389867019774243

const b = r.normal(1, 5) // returns a number between 1 and 5 (greater than 1, less than 5)

const values = range(100000).map(_ => r.normal(0, 10))

// 0 -  1: 3
// 1 -  2: 136
// 2 -  3: 2143
// 3 -  4: 13704
// 4 -  5: 34150
// 5 -  6: 34161
// 6 -  7: 13452
// 7 -  8: 2127
// 8 -  9: 120
// 9 - 10: 4

r.probability(p)

Returns true or false, with p probability of being true (where p is a percentage between 0% and 100%).

const a = r.probability(0.1) // 10% chance of returning `true`, 90% chance of returning `false`.
// a = false

r.coinFlip()

Returns true or false, with an even chance of being either one.

const a = r.coinFlip() // 50% chance of returning `true`, 50% chance of returning `false`.
// a = true

r.plusOrMinus()

Returns -1 or 1, with an even chance of being either one.

const a = r.plusOrMinus() // 50% chance of returning -1, 50% chance of returning 1.
// a = -1

r.pick(options)

Returns a randomly selected value from options, with equal probability for each option. The options parameter can be an array or an object.

const a = r.pick({ x: 123, y: 42, z: 10000 })
// a = 42

const b = r.pick(['eeny', 'meeny', 'miney', 'mo'])
// b = 'meeny'

r.alpha(length = 5)

Returns a string of lowercase characters with the given length.

const a = r.alpha()
// a = 'mlqfq'

const b = r.alpha(10)
// b = 'ycttgbxdnh'