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-math

v1.0.5

Published

30+ versatile randomization functions, Python-inspired, UUID, Password generation, covering numbers, arrays, choices, characters, and colors.

Downloads

50

Readme

random-math

NPM package for 30+ versatile randomization functions, Python-inspired, UUID, Password generation, covering numbers, arrays, choices, characters, & colors.

Installation

npm i random-math

for modern JavaScript projects using ESM:

import Random from 'random-math';

Using traditional method for Node.js projects that follow the CommonJS module system:

const Random = require('random-math');

List of all the functions:

Random Generators:

  1. uuid: Generates a Random "Universally Unique Identifier" (UUIDv4).
  2. customUUID: You can customize the UUID you want to randomly generate.
  3. password: Generate a random password with specified criteria (length, complexity).

Random Number Generation:

  1. bool: Generates a random boolean value (true/false).
  2. even: Generate a random even number within a range.
  3. odd: Generate a random odd number within a range.
  4. prime: Generate a random prime number within a range (computationally expensive).
  5. float: Generates a Random floating-point number within a Range.
  6. int: Generates a Random Integer within a Range.
  7. multiFloat: Generates an array of n random numbers between specific range.

String Operations:

  1. stringFromChars: Generates a random string of 12 characters.
  2. stringFromChars: Generates a Random String with Custom Characters.
  3. choice: Picks a random character from a string.
  4. sample: Randomly picks n number of random characters from a string.

Array Operations:

  1. subset: Generate a random subset of elements from an array.
  2. powerSet: Generate the Power Set of the given Set.
  3. permute: Generate all possible permutations of an array (computationally expensive for large arrays).
  4. shuffle: Randomly shuffles an array.
  5. choice: Picks a random item from an array.

Color Manipulation:

  1. complimentaryColor: Generate a random color complementary to a given color.
  2. grayscale: Generate random grayscale color.
  3. randomShade: Generate a random shade (darker version) of a given color.
  4. randomTint: Generate a random tint (lighter version) of a given color.
  5. hex: Generates a random Hex color code.
  6. rgb: Generates a random RGB color.
  7. hsl: Generate a random HSL color.

Gaming Operations:

  1. coinFlip: Randomly return "heads" or "tails" with custom probabilities.
  2. rollDice: Simulate rolling a die with a specified number of sides.
  3. shuffleDeck: Shuffle a deck of cards (array of card objects).
  4. pickCard: Returns a random card from a standard deck of 52 cards.

Usage

uuid: Generates a Random "Universally Unique Identifier" (UUIDv4).

const randomUUID = Random.uuid();
console.log(randomUUID);
// Output will be a string in the format "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"

Example Random Output: f317952b-01fc-4442-8533-219470b5a20b

customUUID: You can customize the UUID you want to randomly generate

  • Example 1:
const customId = Random.customUUID("xxxx-xxxx-xxxx-xxxx");
console.log(customId);

Example Random Output: 4e4e-6730-536a-7b45

  • Example 2:
const customCode = Random.customUUID("yyy_yyy_yyy");
console.log(customCode);

Example Random Output: 1b2_79b_782

  • Example 3:
const customToken = Random.customUUID("xx-xx-xx-yy-xxxxxxxx");
console.log(customToken);
  • Example Random Output: 12-92-28-9f-5c219e65

password: Generate a random password with specified criteria (length, complexity).

const lowPassword = Random.password(6, "low");
console.log(lowPassword);     // example output:  f8iis2
const mediumPassword = Random.password(10, "medium");
console.log(mediumPassword);  // example output:  lwf5cn$ePw
const highPassword = Random.password(16, "high");
console.log(highPassword);    // example output:  "{)#lhI>N4"xiW*@

stringFromChars: Generates a random string of 12 characters

const randomString = Random.stringFromChars(undefined, 12);
console.log(randomString);
  • Example Random Output: 13z1ayayz2ab

stringFromChars: Generates a Random String with Custom Characters:

const specialChars = "!@#$%^&*";
const randomString = Random.stringFromChars(specialChars, 15);
console.log(randomString);

// Generates a random string of 15 characters from special characters
  • Example Random Output: @^#&^&@*$%!*^$$

multiFloat: Generates an array of n random numbers between specific range.

const randomNumbers = Random.multiFloat(0, 1, 30);  // 30 random numbers between 0 and 1
console.log(randomNumbers);

float: Generates a Random floating-point number within a Range

const randomFloat = Random.float(65, 98);   // range is 65-98
console.log(randomFloat);

int: Generates a Random Integer within a Range

const randomInt = Random.int(10, 20);   // range is 10-20
console.log(randomInt);

shuffle: Randomly shuffles an array

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const shuffledNumbers = Random.shuffle(numbers);
console.log(shuffledNumbers);
  • Example Random Output: [6, 4, 1, 7, 5, 2, 9, 8, 3]

choice: Picks a random item from an array

const cricketer = ["virat", "dhoni", "sachin", "ashwin", "bumrah"];
const randomcrick = Random.choice(cricketer);
console.log(randomcrick);

choice: Picks a random character from a string

const message = "Hello, world!";
const randomChar = Random.choice(message);
console.log(randomChar);

sample: Randomly picks n number of random characters from a string

const alphabet = "abcdefghijklmnopqrstuvwxyz";
const randomSample = Random.sample(alphabet, 3);    // here, n = 3
console.log(randomSample);

// It will Pick 3 random characters from the string, "alphabet"
  • Example Random Output: [ 'w', 'l', 'r' ]

hex: Generates a random Hex color code

const randomHex = Random.hex();
console.log(randomHex);
// Outputs a random hex color code (e.g., #F2A34B)
  • Example Random Output: #252096

rgb: Generates a random RGB color

const randomRGB = Random.rgb();
console.log(randomRGB);
  • Example Random Output: rgb(211, 215, 118)

hsl: Generate a random HSL color

const randomHSL = Random.hsl();
console.log(randomHSL);
  • Example Random Output: hsl(313, 91%, 25%)

grayscale: Generate random grayscale color.

console.log(Random.grayscale());
// returns a random grayscale color in RGB format
  • Example Random Output: rgb(139,139,139)

complimentaryColor : Generate a random color complementary to a given color.

console.log(Random.complimentaryColor("rgb(100,200,250)"));
// returns - rgb(250,150,100)

console.log(Random.complimentaryColor("hsl(120,50%,50%)"));
// returns - hsl(300,50.00%,50.00%)

console.log(Random.complimentaryColor("#ffffff"));
// returns - #000000

randomShade: Generate a random shade (darker version) of a given color.

console.log(Random.randomShade("#ff0000"));
// Output a random shade of red
  • Example Random Output: #d90000

randomTint: Generate a random tint (lighter version) of a given color.

console.log(Random.randomTint("#ff0000"));
// Output a random tint of red
  • Example Random Output: #ff2525

coinFlip: Randomly return "heads" or "tails" with custom probabilities.

  • Unbiased Coin - Randomly returns "heads" or "tails" with equal probability (0.5) or half.
const randomOutcome = Random.coinFlip();
console.log(randomOutcome);

Example Random Output: tails

  • Biased Coin - Returns "heads" with the given X% probability and "tails" with (100-X)% probability
// Flip a coin with a 70% chance of heads
const biasedOutcome = Random.coinFlip(0.7);
// Returns "heads" with 70% probability and "tails" with 30% probability
console.log(biasedOutcome);

Example Random Output: heads

bool: Generates a random boolean value (true/false).

const randomBool = Random.bool();
console.log(randomBool);

subset: Generates a random subset of elements from an array.

const numbers = [1, 2, 3, 4, 5, 6];

// Generate a random subset with size 3 (can vary)
const randomSubset = Random.subset(numbers, 3);
console.log(randomSubset);

Example Random Output: [ 6, 4, 2 ]

powerSet: Generates the Power Set of the given Set

const colors = ["red", "green", "blue"];
const allColorCombinations = Random.powerSet(colors);
console.log(allColorCombinations);

Example Random Output: [ [], [ 'red' ], [ 'green' ], [ 'red', 'green' ], [ 'blue' ], [ 'red', 'blue' ], [ 'green', 'blue' ], [ 'red', 'green', 'blue' ] ]

const ayaa = [1, 2, 3];
const powerAyaa = Random.powerSet(ayaa);
console.log(powerAyaa);

Example Random Output: [ [], [ 1 ], [ 2 ], [ 1, 2 ], [ 3 ], [ 1, 3 ], [ 2, 3 ], [ 1, 2, 3 ] ]

permute: Generates all possible permutations of an array.

NOTE: It can be computationally expensive for large arrays

const letters = ["x", "y", "z"];
// Generate all permutations of the letters array
const allPermutations = Random.permute(letters);
console.log(allPermutations);

// Output will show all possible orderings of the letters

Example Random Output: [ [ 'x', 'y', 'z' ], [ 'x', 'z', 'y' ], [ 'y', 'x', 'z' ], [ 'y', 'z', 'x' ], [ 'z', 'x', 'y' ], [ 'z', 'y', 'x' ] ]

rollDice: Generates a random simulated result of rolling a die with a specified number of sides.

  1. To roll a standard 6-sided die:
const roll1 = Random.rollDice();
console.log(roll1);     // output -> 1 to 6
  1. To roll a 10-sided die:
const roll2 = Random.rollDice(10);
console.log(roll2);     // output -> 1 to 10

shuffleDeck: Randomly shuffle a deck of cards (array of card objects).

const deck = [
	{ value: "Ace", suit: "Spades" },
	{ value: "King", suit: "Hearts" },
	{ value: "Queen", suit: "Diamond" },
	// ... (add more card objects to the deck)
  ];
  
  const shuffledDeck = Random.shuffleDeck(deck);
  console.log(shuffledDeck);
  // Random Output: An array of card objects in a shuffled order

pickCard: Returns a random card from a standard deck of 52 cards

const randomCard = Random.pickCard();
console.log(randomCard);
  • Example Random Output: { card: 'King', suit: 'Diamonds' }

even: Generate a random even number within a range.

// Generate even number between 20 and 50 (inclusive)
const randomEvenNumber = Random.even(20, 50);
console.log(randomEvenNumber);
  • Example Random Output: 44

odd: Generate a random odd number within a range.

// Generate odd number between 15 and 35 (inclusive)
const randomOddNumber = Random.odd(15, 35);
console.log(randomOddNumber);
  • Example Random Output: 17

prime: Generate a random prime number within a range (computationally expensive).

// Generate prime number between 50 and 400 (inclusive)
const randomPrimeNumber = Random.prime(50, 400);
console.log(randomPrimeNumber);
  • Example Random Output: 367