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

go-random

v1.0.3

Published

Random value generation utilities

Downloads

341

Readme

Go Random

Random value generation utilities

codecov.io Code Coverage jsdoc donation

  • version: 1.0.3
  • license: GNU LGPLv3

Installation

npm i go-random

or

yarn add go-random

Usage

ES6

import { randInt } from "go-random"

randInt(3); // => generates an integer between 0 - 3
randInt(1, 3); // => generates an integer between 1 - 3

Node

const { randInt } = require("go-random");

randInt(3); // => generates an integer between 0 - 3
randInt(1, 3); // => generates an integer between 1 - 3

Web browser

<script src="dist/go-random.min.js"></script>
<script>
    const { randInt } = Random;

	randInt(3); // => generates an integer between 0 - 3
	randInt(1, 3); // => generates an integer between 1 - 3
</script>

Documentation

Table of Contents

randBool

Generates a random boolean value, either true or false.

Examples

randBool(); // => true or false

Returns boolean The randomly generated boolean value.

Meta

  • since: 1.0.0

randInt

Generates a random integer between the inclusive min and max bounds. If only one argument is provided, an integer between 0 and the given number is returned. If min or max is not a number, the value is first coerced to a number.

Parameters

  • min number The lower bound (inclusive). (optional, default 0)
  • max number The upper bound (inclusive). (optional, default Number.MAX_SAFE_INTEGER)

Examples

randInt(); // => an integer between 0 and 9007199254740991

randInt(2); // => an integer between 0 and 2 (0, 1 or 2)

randInt(-1, 2); // => an integer between -1 and 2 (-1, 0, 1 or 2)
  • Throws TypeError if min or max is not a valid numeric value (NaN).

Returns number The randomly generated integer.

Meta

  • since: 1.0.0

randFloat

Generates a random number between the inclusvie min and max bounds. If only one argument is provided, a number between 0 and the given number is returned. If min or max is not a number, the value is first coerced to a number.

Parameters

  • min number The lower bound (inclusive). (optional, default 0)
  • max number The upper bound (inclusive). (optional, default Number.MAX_VALUE)
  • decimalPlaces number The number of decimal places to have. (optional, default 2)

Examples

randFloat(); // => a number between 0 and 1.7976931348623157E+308

randFloat(2.3); // => a number between 0 and 2.3

randFloat(-1.2, 2.3); // => a number between -1.2 and 2.3

randFloat(-1.2, 2.3, 2); // => a number between -1.2 and 2.3 truncated to 2 decimal places
  • Throws TypeError if min or max is not a valid numeric value (NaN).

Returns number The randomly generated floating point number.

Meta

  • since: 1.0.0

randStr

Generates a random string of the specified length, optionally using the characters provided. If no characters can be obtained from chars, an empty string is returned. If length is not a number, the value is first coerced to a number.

Parameters

  • length number The length of the string. (optional, default DEFAULT_STRING_LENGTH)
  • chars (string | Array<string>) The characters to use to construct the random string. (optional, default DEFAULT_STRING_CHARS)

Examples

randStr(); // => a string that has 8 alphanumeric characters 

randStr(5); // => a string that has 5 alphanumeric characters

randStr(5, "0123456789"); // => a string that has 5 digits
  • Throws TypeError if length is not a valid numeric value (NaN).
  • Throws RangeError if length is a negative number.

Returns string The randomly generated string.

Meta

  • since: 1.0.0

randDate

Generates a Date object whose time is between the inclusive min and max bounds. If only one argument is provided, a date between January 1, 1970 UTC and the given date is returned.

Parameters

  • min (Date | number | string) The lower bound (inclusive). (optional, default 0)
  • max (Date | number | string) The upper bound (inclusive). (optional, default Date.now())

Examples

randDate(); // => a date/time between 1970-01-01 and now

randDate("2012-01-01"); // => a date/time between 1970-01-01 and 2012-01-01

randDate(Date.now(), Date.now() + 3600000); // => a date/time between now and 1 hour from now.
  • Throws TypeError if min or max is a not valid date/time value.

Returns Date The randomly selected date within the date range.

Meta

  • since: 1.0.0

randIndex

Randomly picks an index of the given collection. The probability of each index being picked can be defined with the ratios. A higher ratio value means a higher probability of being picked.

Parameters

  • collection (Array | Object | Iterable | string) The collection to randomly pick an index from.
  • ratios (Array<number> | Object<string, number>)? The ratio values that define the probability of the corresponding index is likely to be picked.

Examples

Without ratios - equal probability

// 0, 1 and 2 are all equally likely to be picked.
randIndex(["a", "b", "c"]);

randIndex({1: "a", 2: "b", 3: "c"});

With ratios - defined probabilities

// 0 is twice as likely to be picked than 1 or 2.
randIndex(["a", "b", "c"], [2, 1, 1]);

randIndex({a: "a", b: "b", c: "c"}, {a: 2, b: 1, c: 1});

Returns any The randomly picked index.

Meta

  • since: 1.0.0

randPick

Randomly picks an item from the given collection. The probability of each item being picked can be defined with the ratios. A higher ratio value means a higher probability of being picked.

Parameters

  • collection (Array | Object | Iterable | string) The collection to randomly pick an item from.
  • ratios (Array<number> | Object<string, number>)? The ratio values that define the probability of each item at the corresponding index is likely to be picked.

Examples

Without ratios - equal probability

// "a", "b" and "c" are all equally likely to be picked.
randPick(["a", "b", "c"]);

randPick({1: "a", 2: "b", 3: "c"});

With ratios - defined probabilities

// "a" is twice as likely to be returned than "b" or "c".
randPick(["a", "b", "c"], [2, 1, 1]);

randPick({a: "a", b: "b", c: "c"}, {a: 2, b: 1, c: 1});

Returns any The randomly picked item.

Meta

  • since: 1.0.0