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

randomless

v0.1.3

Published

Tiny generator that produces "human" random numbers

Downloads

13

Readme

Randomless

Computers are bad at randomness and so are people. But computers can imitate human ideas about randomness using this little JavaScript library with no dependencies

Purpose

This library provides small functions that generate random numbers or distributions that look random to people, but in reality are not so.

If you ask a person to predict the order of heads and tails when they flip a coin 10 times, they will probably say something like "HTTHTHTHHT". But in reality there will be many clusters, where heads or tails come one after another several times like "HHTHHHHHTT" or even "TTTTTTTTTT".

This library tries to produce random values with few clusters, like a human would.

Install

Install the library

npm i --save randomless

Use it in your code

const randomless = require('randomless')
// or
import randomless from 'randomless'

You can also import individual functions

import getSequence from 'randomless/sequence'
import getNumbers from 'randomless/numbers'
import getGrid from 'randomless/grid'
import Picker from 'randomless/picker'

Functions

  • randomless.getNumbers(count: number): number[]
    randomless.getNumbers(count: number, max: number): number[]
    randomless.getNumbers(count: number, min: number, max: number): number[]

    Produces an array of length count of random(less) numbers between min (inclusive, default 0) and max (not inclusive, default 1). Numbers will be distributed more or less evenly around the range. Array is guaranteed to be sorted

    Comparison

    "Truly" random numbers

    console.log(
      // Creating an array of 5 elements and filling it with
      // numbers from 0 to 20 and rounding to two decimal digits
      [...Array(5)].map(() => Math.floor(Math.random() * 20 * 100) / 100).sort()
    )
    // [6.81, 13.22, 17.33, 19.4, 19.84]

    Randomless numbers

    console.log(
      // Also rounding to two decimal digits
      randomless.getNumbers(5, 20).map((n) => Math.floor(n * 100) / 100)
    )
    // [1.71, 6.09, 9.16, 12.69, 18.7]
  • randomless.getGrid(count: number, dimensions: number[]): number[]

    Produces a grid with any amount of dimensions that is filled with zeros and contains an amount of ones, specified by count, that are distributed random(less)ly around it.

    Comparison

    In this example I generate 2 grids, where zeros and ones are represented as '—' and '*' symbols respectively.

    "Truly" random grid

    // Creating 8x30 array, filled with '—'
    const grid = [...Array(8)].map(() => [...Array(30)].fill('—'))
    // Randomly placing asteriks around
    for (let i = 0; i < 15; i++) {
      const row = Math.floor(Math.random() * 8)
      const col = Math.floor(Math.random() * 30)
      grid[row][col] = '*'
    }
    // Transforming array into a string
    console.log(grid.map((row) => row.join('')).join('\n'))
    /*
    ——*—————————*————*————————————
    ——————————————***—————————————
    ——————————————————————————*—*—
    ———————————————————————————*——
    ——————————————————————————————
    —————————————————————————*————
    —————————*————————————————————
    ——————————*————————————————*——
    */

    Randomless grid

    const rgrid = randomless.getGrid(15, [8, 30])
    console.log(
      // Transforming array into a string
      rgrid.map((row) => row.map((col) => (col ? '*' : '—')).join('')).join('\n')
    )
    /*
    ————————*—————————————————————
    ——*—————————————————————*———*—
    ————*———————*———————*—————————
    ———————————————*——————————————
    ——————*———————————————————————
    ——————————————————————*———————
    ——*———————————*———*———————————
    ——————————*————*——————————————
    */
  • randomless.getSequence<T>(count: number, symbols: T[]): T[]

    Produces a random(less) sequence of length, given by count, that consists of given symbols.

    Comparison

    Example with heads and tails from the description of the library:

    "Truly" random distribution

    console.log(
      // Creating array with 10 elements and filling it randomly with 'T' or 'H'
      [...Array(10)].map(() => ['T', 'H'][Math.round(Math.random())]).join('')
    )
    // "HHHHHHTTTT"

    Randomless distribution

    console.log(randomless.getSequence(10, ['H', 'T']).join(''))
    // "HTTHHTHTTT"

The library also provides a small class that generates such sequences infinitely

class Picker<T> {
  constructor(symbols: T[])
  next(): T
}

Example

const picker = new Picker(['H', 'T'])

// Print randomless heads and tails till the end of times
while (true) {
  console.log(picker.next())
}

Note, that this class does not implement JavaScript iterator protocol