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

@fast.one/random

v1.0.9

Published

`@fast.one/random` is a library that provides a random number generator with various functionalities. It offers the ability to generate random unsigned integers, random numbers between 0 and 2, retrieve random elements from an array, generate arrays of ra

Downloads

1

Readme

@fast.one/random

@fast.one/random is a library that provides a random number generator with various functionalities. It offers the ability to generate random unsigned integers, random numbers between 0 and 2, retrieve random elements from an array, generate arrays of random values, and more.

Installation

You can install @fast.one/random using npm or yarn:

npm install @fast.one/random

or

yarn add @fast.one/random

Usage

To use the @fast.one/random library, import the Random object from the library:

import { Random } from '@fast.one/random';

The Random object provides the following methods:

uint

uint(size?: number): number;

Generates a random unsigned integer between 0 and the specified size (inclusive). If no size is provided, the default upper bound is 1000.

three

three(): 0 | 1 | 2;

Generates a random number between 0 and 2 (inclusive), representing one of three possible values: 0, 1, or 2.

element

element<T>(array: T[]): T;

Retrieves a random element from the provided array.

arrayOf

arrayOf<T>(min: number, max: number, value: () => T): T[];

Generates an array of random values using the specified value generator function. The min parameter determines the minimum length of the array, and the max parameter determines the maximum length of the array.

asyncArrayOf

asyncArrayOf<T>(min: number, max: number, value: () => Promise<T>): Promise<T[]>;

Generates an array of asynchronously generated random values using the specified promise-based value generator function. The min parameter determines the minimum length of the array, and the max parameter determines the maximum length of the array.

coin

coin(): boolean;
coin<T>(value: () => T): T | undefined;
coin<T>(value: () => Promise<T>): Promise<T | undefined>;

Simulates a coin toss with a 50% probability of returning true for heads and false for tails. The coin method can also accept a value generator function as a parameter. If the coin lands on heads, it will return the generated value; otherwise, it will return undefined. This method can also handle promise-based value generation.

outOf

outOf(n: number): boolean;
outOf<T>(n: number, value: () => T): T | undefined;
outOf<T>(n: number, value: () => Promise<T>): Promise<T | undefined>;

Generates a random number between 1 and the specified number (inclusive) and returns true if it equals 1. The outOf method can also accept a value generator function as a parameter. If the generated number equals 1, it will return the generated value; otherwise, it will return undefined. This method can also handle promise-based value generation.

Example

Here's an example of how to use the @fast.one/random library:

import { Random } from '@fast.one/random';

const randomValue = Random.uint(100); // Generate a random unsigned integer between 0 and 100

const randomElement = Random.element([1, 2, 3, 4, 5]); // Get a random element from the array

const randomArray = Random.arrayOf(5, 10, () => Random.uint()); // Generate an

 array of random unsigned integers

const asyncRandomArray = await Random.asyncArrayOf(5, 10, async () => {
  // Generate asynchronously a random value
  return new Promise<number>((resolve) => {
    setTimeout(() => {
      resolve(Random.uint());
    }, 1000);
  });
});

const coinResult = Random.coin(); // Simulate a coin toss

const coinResultWithValue = Random.coin(() => 'Heads'); // Simulate a coin toss with a specific value

const outOfResult = Random.outOf(10); // Check if a random number is equal to 1

const outOfResultWithValue = Random.outOf(10, () => 'Value'); // Check if a random number is equal to 1 and return a specific value

License

This library is released under the MIT License.