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

jsrand

v0.2.5

Published

A random module for JavaScript, generate random numbers follow different random distributions, including Uniform, Gaussian, Poisson, Exponential, Gamma, Lognormal, Cauchy, Chisquared, Binomial, Weibull and so on

Downloads

8

Readme

JSrandom

Random module for JavaScript

JSrandom.randFloat([from,] to)

Arguments

  from (Number): The lower bound of range, defaut is zero.
  to (Number): The upper bound of range.

Returns

  (Number): Returns a random number between [from,to).

Example

JSrandom.randFloat(5);// <=> JSrandom.randFloat(0,5)
//=>1.4567891701389055
JSrandom.randFloat(-2,3);
//=>-0.37792440940529315

JSrandom.randInt([from,] to)

Arguments

  from (Number): The lower bound of range, defaut is zero.
  to (Number): The upper bound of range.

Returns

  (Integer): Returns a random integer between [from,to).

Example

JSrandom.randInt(5);// <=> JSrandom.randInt(0,5)
//=>2
JSrandom.randInt(-2,3);
//=>-1

JSrandom.select(array [, generator])

Arguments

  array (ArrayLike): The array to select.
  generator (function): A random number generator.defaut is the build-in uniform generator use MT19937

Returns

  (any): Returns a random element in the array.

Example

JSrandom.select("abcdefg");
//=>"f"
JSrandom.select([0,2,3,5,2,9,1],Math.random);
//=>5

JSrandom.choice(array)

  Equal to JSrandom.select(array), use the built-in random number generator.


JSrandom.sample(array [,number])

Arguments

  array (ArrayLike): The array to select.
  number (Number): The element number to pick, defaut value is a random number between [1,array.length).

Returns

  (Array): A random subset of the array.

Example

JSrandom.sample("abcdefg");
//=>["b", "e", "c", "a", "d", "f", "g"]
JSrandom.sample([0,2,3,5,2,9,1],3);
//=>[5, 0, 2]

JSrandom.shuffle(array)

Arguments

  array (ArrayLike): The array to shuffle.

Returns

  (Array): A shuffled list, do not change the origin array.

Example

JSrandom.shuffle("abcdefg");
//=>"bacdegf"
JSrandom.shuffle([0,2,3,5,2,9,1]);
//=>[0, 3, 2, 9, 5, 2, 1]

Random Constructor


JSrandom.Uniform(lower,upper)

Constructor of the random number generator obey uniform distribution.

Arguments

  lower (Number): The lower bound of range.
  upper (Number): The upper bound of range.

Returns

  (function): An instance of Uniform which generate uniform random numbers between lower and upper.

Example

var generator = JSrandom.Uniform(0,10);
generator();
//=>2.0685795052296387

JSrandom.Gaussian(mu,sigma)

Constructor of the random number generator obey gaussian distribution.

Arguments

  mu (Number): The expectation of gaussian distribution.
  sigma (Number): The variance of gaussian distribution.

Returns

  (function): An instance of Gaussian which generate random number obey normal distribution.

Example

var generator = JSrandom.Gaussian(0,1);
generator();
//=>0.6532581496839591

JSrandom.Bernoulli(prob)

Constructor of the random number generator obey bernoulli distribution.

Arguments

  prob (Number): The probability of true.

Returns

  (function): An instance of Bernoulli which return true or false.

Example

var generator = JSrandom.Bernoulli(0.5);
generator();
//=>true
generator();
//=>false

JSrandom.Binomial(upper,prob)

Constructor of the random number generator obey binomial distribution.

Arguments

  upper (Integer): The upper bound of range.
  prob (Number): The probability of success.

Returns

  (function): An instance of Binomial which return integer [ 0, upper ] , the probability obey the binomial distribution.

Example

var generator = JSrandom.Binomial(10,0.5);
generator();
//=>8
generator();
//=>3

JSrandom.Cauchy(location,scale)

Constructor of the random number generator obey cauchy distribution.

Arguments

  location (Number): The location of cauchy distribution.
  scale (Number): The scale of cauchy distribution.

Returns

  (function): An instance of Cauchy which return random number obey the cauchy distribution.

Example

var generator = JSrandom.Cauchy(3,1);
generator();
//=>5.677625315054479
generator();
//=>-10.669827067906697

JSrandom.Exponential(lamda)

Constructor of the random number generator obey exponential distribution.

Arguments

  lamda (Number): The index coefficient of Exponential distribution.

Returns

  (function): An instance of Exponential which return random number obey the exponential distribution.

Example

var generator = JSrandom.Exponential(3);
generator();
//=>0.21951388774858185
generator();
//=>0.15475914346331018

Other Random Distributions

Lognormal(mu,sigma)

negativeBinomial(times,prob)

Gumbel(location,scale)

Logistic(location,scale)

Geometric(prob)

Gamma(shape,scale)

Chisquared(degree)

Poisson(mean)

Weibull(shape,scale)

Rayleigh(scale)


##Random generator

JSrandom.uniform()

Returns

  (Number): random number between between [0, 1) and obey uniform distribution.

Example

JSrandom.uniform();
//=>0.22049420430347985

JSrandom.gaussian(mu,sigma)

Arguments

  mu (Number): The expectation of gaussian distribution, defaut value is 0.
  sigma (Number): The variance of gaussian distribution, defaut value is 1.

Returns

  (Number): random number obey normal distribution.

Example

JSrandom.gaussian();
//=>1.7041861226289101

JSrandom.bernoulli(prob)

Arguments

  prob (Number): The probability of true, defaut value is 0.5.

Returns

  (Bool): true or false.

Example

JSrandom.bernoulli(0.8);
//=>true

JSrandom.exponential(lamda)

Arguments

  lamda (Number): The index coefficient of Exponential distribution.

Returns

  (Number): random number obey the exponential distribution.

Example

JSrandom.exponential(3);
//=>0.29350827394207385

###Other generator

lognormal(mu,sigma)

cauchy(location,scale)

gumbel(location,scale)

logistic(location,scale)

poisson(mean)

gamma(shape,scale)

chisquared(degree)

geometric(prob)

binomial(up,prob)

weibull(shape,scale)