Main Content

unifrnd

Continuous uniform random numbers

Description

example

r= unifrnd(a,b)generates a random number from the continuous uniform distribution with the lower endpointsaand upper endpointb.

example

r= unifrnd(a,b,sz1,...,szN)generates an array of uniform random numbers, wheresz1,...,szNindicates the size of each dimension.

example

r= unifrnd(a,b,sz)generates an array of uniform random numbers, where the size vectorszspecifiessize(r).

Examples

collapse all

Generate a random number from the continuous uniform distribution with the lower parameter0and upper parameter1.

r = unifrnd(0,1)
r = 0.8147

Generate5random numbers from the continuous uniform distributions on the intervals (0,1), (0,2),..., (0,5).

a1 = 0; b1 = 1:5; r1 = unifrnd(a1,b1)
r1 =1×50.8147 1.8116 0.3810 3.6535 3.1618

By default,unifrndgenerates an array that is the same size asaandbafter any necessary scalar expansion so that all scalars are expanded to match the dimensions of the other inputs.

If you specify array dimensionssz1,...,szN, they must match the dimensions ofaandbafter any necessary scalar expansion.

Generate a 2-by-3 array of random numbers from the continuous uniform distribution with the lower parameter0and upper parameter1.

sz = [2 3]; r2 = unifrnd(0,1,sz)
r2 =2×30.0975 0.5469 0.9649 0.2785 0.9575 0.1576

Generate 6 random numbers on the intervals (0,1), (1,2),..., (5,6).

a3 = 0:5; b3 = 1:6; r3 = unifrnd(a3,b3,1,6)
r3 =1×60.9706 1.9572 2.4854 3.8003 4.1419 5.4218

Input Arguments

collapse all

Lower endpoint of the uniform distribution, specified as a scalar value or an array of scalar values.

To generate random numbers from multiple distributions, specifyaandbusing arrays. If bothaandbare arrays, then the array sizes must be the same. If eitheraorbis a scalar, thenunifrndexpands the scalar argument into a constant array of the same size as the other argument.Each element inris the random number generated from the distribution specified by the corresponding elements inaandb.

Example:[0 -1 7 9]

Data Types:single|double

Upper endpoint of the uniform distribution, specified as a scalar value or an array of scalar values.

To generate random numbers from multiple distributions, specifyaandbusing arrays. If bothaandbare arrays, then the array sizes must be the same. If eitheraorbis a scalar, thenunifrndexpands the scalar argument into a constant array of the same size as the other argument.Each element inris the random number generated from the distribution specified by the corresponding elements inaandb.

Example:[1 1 10 10]

Data Types:single|double

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

If eitheraorbis an array, then the specified dimensionssz1,...,szNmust match the common dimensions ofaandbafter any necessary scalar expansion. The default values ofsz1,...,szNare the common dimensions.

  • If you specify a single valuesz1, thenr如果是一个方阵zesz1-by-sz1.

  • If the size of any dimension is0or negative, thenris an empty array.

  • Beyond the second dimension,unifrndignores trailing dimensions with a size of 1. For example,unifrnd(–3,5,3,1,1,1)produces a 3-by-1 vector of random numbers from the uniform distribution with lower endpoint –3 and upper endpoint 5.

Example:2,3

Data Types:single|double

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

If eitheraorbis an array, then the specified dimensionsszmust match the common dimensions ofaandbafter any necessary scalar expansion. The default values ofszare the common dimensions.

  • If you specify a single value [sz1], thenr如果是一个方阵zesz1-by-sz1.

  • If the size of any dimension is0or negative, thenris an empty array.

  • Beyond the second dimension,unifrndignores trailing dimensions with a size of 1. For example,unifrnd(–3,5,[3 1 1 1])produces a 3-by-1 vector of random numbers from the uniform distribution with lower endpoint –3 and upper endpoint 5.

Example:[2 3]

Data Types:single|double

Output Arguments

collapse all

Uniform random numbers, returned as a scalar value or an array of scalar values with the dimensions specified bysz1,...,szNorsz.Each element inris the random number generated from the distribution specified by the corresponding elements inaandb.

Alternative Functionality

  • unifrndis a function specific to the continuous uniform distribution. Statistics and Machine Learning Toolbox™ also offers the generic functionrandom支持各种概率金宝appability distributions. To userandom, create aUniformDistributionprobability distribution object and pass the object as an input argument or specify the probability distribution name and its parameters. Note that the distribution-specific functionunifrndis faster than the generic functionrandom.

  • Userandto generate numbers from the uniform distribution on the interval(0,1).

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

Extended Capabilities

Version History

Introduced before R2006a