freude
freude

Reputation: 3832

Random number with user-defined distribution function (MATLAB)

Is there a way one could get the random number generation satisfying a user-determined distribution function. In MATLAB, I have found random number generators with uniform and normal distribution functions only.

Upvotes: 2

Views: 1846

Answers (2)

Luis Mendo
Luis Mendo

Reputation: 112659

The Statistics Toolbox has a large number of random number generators with predefined distributions. Hopefully the ones you need are contained in the list:

Random Number Generators.

  betarnd     - Beta random numbers.
  binornd     - Binomial random numbers.
  chi2rnd     - Chi square random numbers.
  evrnd       - Extreme value random numbers.
  exprnd      - Exponential random numbers.
  frnd        - F random numbers.
  gamrnd      - Gamma random numbers.
  geornd      - Geometric random numbers.
  gevrnd      - Generalized extreme value random numbers.
  gprnd       - Generalized Pareto inverse random numbers.
  hygernd     - Hypergeometric random numbers.
  iwishrnd    - Inverse Wishart random matrix.
  johnsrnd    - Random numbers from the Johnson system of distributions.
  lognrnd     - Lognormal random numbers.
  mhsample    - Metropolis-Hastings algorithm.
  mnrnd       - Multinomial random vectors.
  mvnrnd      - Multivariate normal random vectors.
  mvtrnd      - Multivariate t random vectors.
  nbinrnd     - Negative binomial random numbers.
  ncfrnd      - Noncentral F random numbers.
  nctrnd      - Noncentral t random numbers.
  ncx2rnd     - Noncentral Chi-square random numbers.
  normrnd     - Normal (Gaussian) random numbers.
  pearsrnd    - Random numbers from the Pearson system of distributions.
  poissrnd    - Poisson random numbers.
  randg       - Gamma random numbers (unit scale).
  random      - Random numbers from specified distribution.
  randsample  - Random sample from finite population.
  raylrnd     - Rayleigh random numbers.
  slicesample - Slice sampling method.
  trnd        - T random numbers.
  unidrnd     - Discrete uniform random numbers.
  unifrnd     - Uniform random numbers.
  wblrnd      - Weibull random numbers.
  wishrnd     - Wishart random matrix.

You could also use random, which basically calls one of the above functions depending of the name of the distribution:

RANDOM Generate random arrays from a specified distribution.

  R = RANDOM(NAME,A) returns an array of random numbers chosen from the
  one-parameter probability distribution specified by NAME with parameter
  values A.

  [...]

  NAME can be:

    'beta'  or 'Beta',
    'bino'  or 'Binomial',
    'chi2'  or 'Chisquare',
    'exp'   or 'Exponential',
    'ev'    or 'Extreme Value',
    'f'     or 'F',
    'gam'   or 'Gamma',
    'gev'   or 'Generalized Extreme Value',
    'gp'    or 'Generalized Pareto',
    'geo'   or 'Geometric',
    'hyge'  or 'Hypergeometric',
    'logn'  or 'Lognormal',
    'nbin'  or 'Negative Binomial',
    'ncf'   or 'Noncentral F',
    'nct'   or 'Noncentral t',
    'ncx2'  or 'Noncentral Chi-square',
    'norm'  or 'Normal',
    'poiss' or 'Poisson',
    'rayl'  or 'Rayleigh',
    't'     or 'T',
    'unif'  or 'Uniform',
    'unid'  or 'Discrete Uniform',
    'wbl'   or 'Weibull'.

Upvotes: 3

Shai
Shai

Reputation: 114796

You can always use randsample with weighted seampling. Suppose you want to sample k numbers in range 1:n with probability p (where p is a non-negative vector of length n) then

randsample( n, k, true, p );

Upvotes: 2

Related Questions