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

@manutero/randomjs

v0.3.0

Published

Humanized random functions

Downloads

13

Readme

BlueBall semantic-release

@manutero/randomjs

Humanized random functions.

Installation

npm i @manutero/randomjs

...or just copy one file to your project:

curl -s https://raw.githubusercontent.com/manutero/randomjs/main/index.js > {my-awesome-project}/src/random.js

Quick Start

const { RandomGenerator } = require("@manutero/randomjs");

const random = RandomGenerator();

random.number(1, 10); // natural number between [a,b]
// => 3

random.normal(1, 10); // natural number between [a,b] considering a normal distribution
// => 6

random.roll("2d6"); // roll some dices
// => [2, 5]

random.bet(42.4); // bet with (0..100%) chance of success.
// => false

random.unit(); // random unit (0 - 1)
// => 0.7580578515771776

random.pickOne([1, 2, 3, 4, 5]); // pick one element (all elements have same weight)
// => 1

random.pickOne([
  // pick one element (considering different weights)
  { key: 1, weight: 1 },
  { key: 2, weight: 1 },
  { key: 3, weight: 9999 },
  { key: 4, weight: 1 },
]);
// => {key: 3, weight: 9999}

Dependencies

None. Just a plain .js file wrapping Math.random()

Use Case

Some uses cases I can think of...

  • you just want a natural random between 1 and 10 while you aren't concern about pure mathematical correctness.
  • you just want to pick an element from an array while not adding another dependency to your project (but copying 1 easy-to-read js file is ok)
  • you just want to roll some dices while would be great that other guy has tested that function already.

If you're looking for a mathematically correct Random generator OR cryptographically secure random numbers, try with random-js instead

Method Detail

.number(a:int, b: int) -> int

Natural number between [a, b] (inclusive).

> random.number(1,10)
6
> random.number(1,10)
3
> random.number(1,10)
9
> random.number(1,10)
9
> random.number(1,10)
10

.normal(a: int, b: int, {skew?: int}) -> int

natural number between [a, b] (inclusive) considering a normal distribution.

> random.normal(1,10)
5
> random.normal(1,10)
4
> random.normal(1,10)
6
> random.normal(1,10)
4
> random.normal(1,10)
6
> random.normal(1,10)
6
> random.normal(1,10)
5

.roll(dice: str) -> [number]

Roll some dices

> random.roll('1d6')
[5]
> random.roll('2d12')
[4, 10]
> random.roll('3d6')
[1, 1, 5]
> random.roll('D3')
[2]
> random.roll('1D12')
[12]

.bet(options: float) -> boolean

bet with a percentage of success (expected a number between 0 - 100)

> random.bet(40.0)
false
> random.bet(40.0)
true
> random.bet(40.0)
false
> random.bet(0.0)
false
> random.bet(100.0)
true

.unit() -> float

unit random between (0, 1)

> random.unit()
0.7580578515771776
> random.unit()
0.11270887637510896
> random.unit()
0.028320789337158203

.pickOne(choices: [T], opts?: { weightKey>: string}) -> T:

pick one element from an array

> random.pickOne([1 ,2, 3, 4, 5])
3
> random.pickOne([1 ,2, 3, 4, 5])
5
> random.pickOne([1 ,2, 3, 4, 5])
5
> random.pickOne([1 ,2, 3, 4, 5])
1

Pick one element from an array with weight

> random.pickOne([
    { key: 1, weight: 10 },
    { key: 2, weight: 10 },
    { key: 3, weight: 10 },
    { key: 4, weight: 10 },
    { key: 5, weight: 10 }
])
{ key: 5, weight: 10 }
> random.pickOne([
    { key: 1, weight: 10 },
    { key: 2, weight: 10 },
    { key: 3, weight: 10 },
    { key: 4, weight: 99999 },
    { key: 5, weight: 10 }
])
{ key: 4, weight: 99999 }

.seed

Get the internal seed

> random.seed
72779816406.94533

constructor()

Force the internal seed

> r1 = RandomGenerator(42)
> r2 = RandomGenerator(42)
> r1.unit()
0.6011037519201636
> r2.unit()
0.6011037519201636
> r1.unit() === r2.unit()
true

License

MIT