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

js-randomize

v2.0.0

Published

A simple javascript utility to generate random values in your code

Downloads

39

Readme

js-randomize

🚀 Publish

⚠️ Breaking Changes in Version 2.0.0
Version 2.0.0 of js-randomize introduces breaking changes. Please refer to the CHANGELOG for details on what has changed and how to adapt your code.

Table of Contents

Overview

js-randomize is a lightweight TypeScript library that provides utility functions for generating random integers, floats, booleans, arrays, and strings. It supports custom random number generators and is perfect for applications like games, testing, or simulations.

Installation

To install using pnpm:

pnpm add js-randomize

Or, using npm:

npm install js-randomize

Usage

Importing

Since the package is now ESM-only, use the import syntax to include it in your project:

import { Randomize } from 'js-randomize';

const randomize = new Randomize(); // Use the default Math.random() or pass a custom random generator

Generating Random Integers

const randomInt = randomize.integer(1, 10);
// randomInt is an integer between 1 and 10 (inclusive)

Generating Random Floats

const randomFloat = randomize.float(0, 1, 3);
// randomFloat is a float between 0 and 1 with 3 decimal places

Generating an Array of Random Integers

const randomIntArray = randomize.intArray(5, 1, 100);
// randomIntArray is an array of 5 random integers between 1 and 100

Generating an Array of Random Floats

const randomFloatArray = randomize.floatArray(5, 0, 1, 2);
// randomFloatArray is an array of 5 random floats between 0 and 1 with 2 decimal places

Generating a Random Boolean

const randomBool = randomize.boolean();
// randomBool is either true or false

Picking a Random Element from an Array

const array = ['apple', 'banana', 'cherry'];
const randomElement = randomize.pick(array);
// randomElement is a random item from the array

Sampling Multiple Non-Repeating Elements from an Array

const randomSample = randomize.sample(array, 2);
// randomSample contains 2 random non-repeating items from the array

Shuffling an Array

const shuffledArray = randomize.shuffle(array);
// shuffledArray is a shuffled version of the original array

Generating a Random String

const randomString = randomize.string(8);
// randomString is a random 8-character string (alphanumeric by default)

Using a Custom Random Generator

You can also provide a custom random generator function:

const customRandomFn = () => 0.5;
const customRandomize = new Randomize(customRandomFn);

const randomInt = customRandomize.integer(1, 10);
// With the custom random function, the result will always be 5

TypeScript Usage

import { Randomize } from 'js-randomize';

const randomize = new Randomize();
const randomInt: number = randomize.integer(1, 10);

Contributing

Pull requests are welcome! For major changes, please open an issue first to discuss what you'd like to change. Make sure to update tests as appropriate.

License

MIT