Skip to content

LZ0211/JSrandom

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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)

About

Random module for JavaScript

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published