UNPKG

random-pie

Version:

A lightweight TypeScript/JavaScript library providing Python-style random number generation and randomization utilities. This utility module implements the most common functions from Python's random module, making it intuitive for Python developers workin

85 lines (84 loc) 2.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const number_random_1 = require("./number-random"); const array_random_1 = require("./array-random"); /** * A class providing static methods for random operations */ class Random { /** * Generates a random floating-point number between 0 (inclusive) and 1 (exclusive). * @returns A random floating-point number between 0 and 1 */ static rand() { return (0, number_random_1.rand)(); } /** * Generates a random floating-point number within a given range. * @param min The minimum value of the range * @param max The maximum value of the range * @returns A random floating-point number between min and max */ static uniform(min, max) { return (0, number_random_1.uniform)(min, max); } /** * Generates a random integer from the specified range. * @param args Range parameters (start, stop, step) * @returns A random integer within the specified range */ static randInt(...args) { return (0, number_random_1.randInt)(...args); } /** * Generates a random number from the given range. * @param args Range parameters (start, stop, step) * @returns The random number */ static randRange(...args) { return (0, number_random_1.randRange)(...args); } /** * Return a random element from the given array. * @param arr The input array * @returns An element from the given array */ static choice(arr) { return (0, array_random_1.choice)(arr); } /** * Return a list of randomly selected elements from the given array. * @param arr The input array * @param options Configuration options for selection * @returns An array containing k randomly selected elements */ static choices(arr, options) { return (0, array_random_1.choices)(arr, options); } /** * Shuffles the given array in place. * @param arr The input array */ static shuffle(arr) { (0, array_random_1.shuffle)(arr); } /** * Returns a shuffled copy of the given array. * @param arr The input array * @returns A new array with the same elements, in a random order */ static shuffled(arr) { return (0, array_random_1.shuffled)(arr); } /** * Returns k unique elements chosen from the population sequence. * @param arr The population sequence * @param k The number of elements to choose * @param counts The weights for each element in the population * @returns A list of k unique elements from the population */ static sample(arr, k, counts) { return (0, array_random_1.sample)(arr, k, counts); } } exports.default = Random;