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

@daeinc/random

v0.4.1

Published

Random utilities

Downloads

19

Readme

@daeinc/random

Random utilities. Mainly a collection of convenience functions. The functions are designed to be used with existing random libraries. It has no dependency on any particular random libraries but it's best to use with those seeded ones.

The function signatures are important. The chosen random functions have to support randFn(min, max), randFn(max) and shuffleFn(arr), etc. My current choice is canvas-sketch-util.

Installation

npm i @daeinc/random

then,

import { sample, ... } from "@daeinc/random"

Types

declare type RandFn = (n: number) => number;
declare type ShuffleFn = <T>(arr: T[]) => T[];

Functions

boolean

function boolean<T>(prob: number, randFn: RandFn): boolean;
function boolean<T>(prob: number, randFn: RandFn, optTrue: NonNullable<T>, optFalse: NonNullable<T>): T;

Returns either true or false given a probability, prob between 0 and 1. It can also return custom value.

function booleanFnCreator(randFn: RandFn): (prob: number) => boolean;
function booleanFnCreator<T>(randFn: RandFn, optTrue: NonNullable<T>, optFalse: NonNullable<T>): (prob: number) => T;

Convenience function so as not to input the same seeded function all the time when using boolean(). It returns a seeded boolean function.

sample

const sample: <T>(arr: NonNullable<T>[], randFn: RandFn) => T;

Samples a random element from array with seeded random function

sampleGaussian

const sampleGaussian: <T>(arr: NonNullable<T>[], mean: number | undefined, stddev: number | undefined, gaussianFn: (mean: number, stddev: number) => number) => T;

(This function is not quite ready)

sampleMultiple

const sampleMultiple: <T>(arr: NonNullable<T>[], numSamples: number, shuffleFn: ShuffleFn) => T[];

Samples multiple elements from the original array. Returns a new array.

sampleWeighted

const sampleWeighted: <T>(values: T[], weights: number[], randFn: RandFn) => T;

Samples a random value based on weights array. The value returned can be any type, even an array.

shuffle

const shuffle: <T>(arr: T[], shuffleFn: ShuffleFn) => T[];

Shuffles array. Returns a new array.

To Dos

  • try random-js
  • REVIEW: https://attacomsian.com/blog/javascript-deep-clone-array

License

MIT