Main Content

chi2rnd

Chi-square random numbers

Description

r = chi2rnd(nu) generates a random number from the chi-square distribution with nu degrees of freedom.

example

r = chi2rnd(nu,sz1,...,szN) generates an array of random numbers from the chi-square distribution, where sz1,...,szN indicates the size of each dimension.

example

r = chi2rnd(nu,sz) generates an array of random numbers from the chi-square distribution, where vector sz specifies size(r).

example

Examples

collapse all

Generate a single random number from the chi-square distribution with 10 degrees of freedom.

nu = 10;
r = chi2rnd(nu)
r = 
19.7102

Generate a 1-by-6 array of chi-square random numbers with 1 degree of freedom.

nu1 = ones(1,6);  % 1-by-6 array of ones
r1 = chi2rnd(nu1)
r1 = 1×6

    2.5368    0.2447    0.4314    2.0153    0.0418    4.3486

By default, chi2rnd generates an array that is the same size as nu.

If you specify nu as a scalar, chi2rnd expands nu into a constant array with dimensions specified by sz1,...,szN.

Generate a 2-by-6 array of chi-square random numbers, all with 3 degrees of freedom.

nu2 = 3;
sz1 = 2;
sz2 = 6;
r2 = chi2rnd(nu2,sz1,sz2)
r2 = 2×6

    0.5761    5.3582    1.0124    0.9851    1.0529    3.0765
    7.9240    1.7373    0.6291    7.0240    1.8496    2.2690

If you specify both nu and sz as arrays, then the dimensions specified by sz must match the dimension of nu.

Generate a 1-by-6 array of chi-square random numbers with 3 to 8 degrees of freedom.

nu3 = 3:8;
sz = [1 6];
r3 = chi2rnd(nu3,sz)
r3 = 1×6

    3.9690    7.0961    4.5651    2.4606   13.5038    8.8495

Input Arguments

collapse all

Degrees of freedom for the chi-square distribution, specified as a positive scalar value or an array of positive scalar values.

To generate random numbers from multiple distributions, specify nu using an array. Each element in r is the random number generated from the distribution specified by the corresponding element in nu.

Example: [9 19 49 99]

Data Types: single | double

Size of each dimension, specified as separate arguments of integers.

If nu is an array, then the specified dimensions sz1,...,szN must match the dimensions of nu. The default values of sz1,...,szN are the dimensions of nu.

  • If you specify a single value sz1, then r is a square matrix of size sz1-by-sz1.

  • If the size of any dimension is 0 or negative, then r is an empty array.

  • Beyond the second dimension, chi2rnd ignores trailing dimensions with a size of 1. For example, chi2rnd(5,3,1,1,1) produces a 3-by-1 vector of random numbers from the distribution with five degrees of freedom.

Example: 2,3

Data Types: single | double

Size of each dimension, specified as a row vector of integers.

If nu is an array, then the specified dimensions sz must match the dimensions of nu. The default values of sz are the dimensions of nu.

  • If you specify a single value [sz1], then r is a square matrix of size sz1-by-sz1.

  • If the size of any dimension is 0 or negative, then r is an empty array.

  • Beyond the second dimension, chi2rnd ignores trailing dimensions with a size of 1. For example, chi2rnd(5,[3 1 1 1]) produces a 3-by-1 vector of random numbers from the distribution with five degrees of freedom.

Example: [2 3]

Data Types: single | double

Output Arguments

collapse all

Chi-square random numbers, returned as a scalar value or an array of scalar values with the dimensions specified by sz1,...,szN or sz. Each element in r is the random number generated from the distribution specified by the corresponding element in nu.

Alternative Functionality

  • chi2rnd is a function specific to the chi-square distribution. Statistics and Machine Learning Toolbox™ also offers the generic function random, which supports various probability distributions. To use random, specify the probability distribution name and its parameters. Note that the distribution-specific function chi2rnd is faster than the generic function random.

  • To generate random numbers interactively, use randtool, a user interface for random number generation.

Extended Capabilities

Version History

Introduced before R2006a