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

pkg-random

v2.7.5

Published

None

Downloads

21

Readme

pkg-random

Install

$ npm i pkg-random --save


Usage

const random = require("pkg-random")

Commands List

Variate()

This function returns a random floating point number between 0 and 1.

Usage
  const random = require("pkg-random");
  console.log(random.variate()); // Outputs a random fraction

Choice(array)

This function returns a randomly chosen element from an array.

Usage
  const random = require("pkg-random");
  
  let a = [12,23,34,45,56,67,78,89,90,98,87,76,65,54,43,32,21];
  
  console.log(random.choice(a)); // Outputs a random number from array(a)

This also works for a string instead of arrays.

  const random = require("pkg-random");
  
  let s = "Hello World!";
  
  console.log(random.choice(s)); // Outputs a random character from the string(s)

Returns undefined if the argument is neither an array nor a string.


Choices(array, n)

This function returns an array of n elements from the array. The elements might be repeated.

Usage
  const random = require("pkg-random");
  
  let a = [12,23,34,45,56,67,78,89,90,98,87,76,65,54,43,32,21]
  console.log(random.choices(a, 4)) // Outputs an array of 4 randomly chosen elements from array(a)

This also works for a string instead of arrays.

  const random = require("pkg-random");
  
  let s = "Hello World!"
  console.log(random.choices(s, 4)) // Outputs an array of 4 randomly chosen elements from string(s)

Returns undefined if the argument is neither an array nor a string.


Randint(min, max)

This function returns a random integer from the specified range including the end points specified i.e. from the interval [ min , max ]

Usage
  const random = require("pkg-random");
  
  console.log(random.randint(-34,50)) // Outputs a random integer between -34 and 50

Your app will be stuck at infinite loop if min > max


Range

This function returns a random integer from the specified range excluding the end points specified i.e. from the interval ( min , max )

Usage
  const random = require("pkg-random");
  
  console.log(random.range(-104,908)) // Outputs a random integer between -104 and 908

Your app will be stuck at infinite loop if min > max


Sample(array, n)

This function returns an array of n unique elements from the array.

Usage
  const random = require("pkg-random");
  
  let a = [12,23,34,45,56,67,78,89,90,98,87,76,65,54,43,32,21]
  console.log(random.sample(a,5)) // Outputs an array of 5 unique elements from a

Difference between choices and sample is that sample picks unique elements while elements can repeat in choices.


Shuffle(array)

This function shuffles the array passed as argument and returns the shuffled array.

Usage
  const random = require("pkg-random");
  
  let a = [12,23,34,45,56,67,78,89,90,98,87,76,65,54,43,32,21]
  console.log(random.shuffle(a)) // Outputs array(a) after shuffling it.

word()

This function returns a random word from the English dictionary.

Usage
const random = require("pkg-random");

console.log(random.word()); // Outputs a random word.

wordArray(min, max)

This function returns an array of minimum min and maximum max randomly selected words.

const random = require("pkg-random");

console.log(random.wordArray(4,8));	// Returns an array of random words.

This returns an array of min words if max is not specified.


sentence(l)

This function returns a sentence made up of l randomly selected words.

Usage
const random = require("pkg-random");

console.log(random.sentence(6)) // Outputs a sentence made up of 6 randomly selected words.

sentenceArray(n, min, max)

This function returns an array of n sentences, each with a minimum of min and maximum of max randomly selected words.

Usage
const random = require("pkg-random");

console.log(random.sentenceArray(5, 4, 8)) // Outputs an array of 5 sentences, each having at least 4 and at most 8 words

This returns an array of n sentences, each with min words if max is not specified.


Tips and Tricks

  • Get a random double between a specified range

    const random = require("pkg-random");
      
    console.log(random.range( min , max ) + random.variate())
  • Get a random positive integer in the range 0 to a random integer between min and max.

    const random = require("pkg-random");
      
    console.log(random.range( min, max ) * random.variate())