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

random-entities

v1.0.9

Published

Randomization utilities in Node or the browser. About as random as you can get natively.

Downloads

14

Readme

Random-Entities

Randomization utilities in Node or the browser. About as random as you can get natively.

Disclaimer: This lib does not claim to be fully cryptographically secure. Use at your own risk. No guarantee of output uniqueness or contextual suitability is given or implied.

Install

npm install crypto-rng

Basic usage

import random from 'random-entities'
random.generate()
// => 0.181188155933931

Methods

Seven methods are provided which can also be imported stand-alone

  • generate
  • char
  • id
  • uuid
  • integer
  • float
  • fromArray

generate

Generates a 52-bit mantissa number, similar to Math.random(), using window.crypto.getRandomNumbers.

RandomEntities.generate(<void>)

Module import: random-entities/core-rng

Returns

<Number> between 0 and 1 (exclusively)

Example

RandomEntities.generate()
// => 0.7676222995416488

char

Generates a string of one or more characters.

RandomEntities.char(length)

Module import: random-entities/char

Arguments

  • length <Number>| optional | default: 1

    The length of the string to generate.

Returns

<String>

A string of random characters.

Example

RandomEntities.char()
// => "z"
RandomEntities.char(12)
// => "bjdzywvmatsb"

id

Generates a unique alpha-numeric identifier string. The first character is always a letter. Optionally, numeric characters can be omitted.

RandomEntities.id(length, alpha)

Module import: random-entities/id

Arguments

  • length <Number> | optional | default: 8 | minimum: 4

    The length of the string to generate.

  • alpha <Boolean> | optional | default: false

    If true, only letters will be returned (equivalent to the char() method)

Returns

<String>

A string of random characters.

Example

RandomEntities.id()
// => "w29a892b"
RandomEntities.id(3)
// => "v489"
RandomEntities.id(6, true)
// => "ryvadn"

uuid

Generates a 128-bit hexadecimal universally unique identifier*

*Although effort has been put into making this as close as possible to a true UUID, no guarantees are made in regards to their true uniqueness. Use at your own risk.

RandomEntities.uuid(<void>)

Module import: random-entities/uuid

Returns

<String>

A 128-bit hexadecimal UUID

Example

RandomEntities.uuid()
// => "ba4eb3e4-9d35-424c-9513-f7cbbc69070b"

integer

Generates a random whole-number integer between the given limits, ceiling optionally inclusive. Limits may be positive or negative numbers, but may not be logically reversed (eg min: 4, max: 2)

RandomEntities.integer(min, max, inclusive)

Module import: random-entities/integer

Arguments

  • min <Number> | required

    The lowest possible result, inclusively

  • max <Number> | required

    The highest possible result, exclusively by default

  • inclusive <Boolean> | optional | default: false

    If true, the highest possible number is included in the result pool

Returns

<Number>

A random number between the given minimum and maximum, exclusively by default.

Example

RandomEntities.integer(0, 100)
// => 41
RandomEntities.integer(1, 2)
// => always 1 since 2 is exclusive
RandomEntities.integer(15, 20, true)
// => 17 but could return 20

float

Generates a random floating-point number between the given limits, ceiling optionally inclusive. Limits may be positive or negative numbers, but may not be logically reversed (eg min: 0.4, max: 0.6)

RandomEntities.float(min, max, prevision, inclusive)

Module import: random-entities/float

Arguments

  • min <Number> | required

    The lowest possible result, inclusively

  • max <Number> | required

    The highest possible result, exclusively by default

  • max <Number> | optional | default: 2

    The number of decimals to keep.

  • inclusive <Boolean> | optional | default: false

    If true, the highest possible number is included in the result pool.

Returns

<Number>

A random floating-point number between the given minimum and maximum, exclusively by default.

Example

RandomEntities.float(2, 3)
// => 2.07
RandomEntities.float(200, 300, 6)
// => 268.887126
RandomEntities.float(0, 10, 1, true)
// => 8.5 but could return 10.0

fromArray

Selects a random entry from an array or string.

RandomEntities.fromArray(list)

Module import: random-entities/from-array

Arguments

  • list <Array|String> | required

    The array from which to select an item.

Returns

<Any>

A random selection from the array.

Example

RandomEntities.fromArray(['one', 'two', 1, 2])
// => "two"
RandomEntities.fromArray([{ prop: 'value' }, [1, 2, 3]])
// => { prop: "value" }
RandomEntities.fromArray('antidisestablishmentarianism')
// => "r"