unifrnd

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

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

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

Examples

Generate Uniform Random Number

Generate a random number from the continuous uniform distribution with the lower parameter 0 and upper parameter 1 .

r = unifrnd(0,1)
r = 0.8147

Generate Uniform Random Numbers

Generate 5 random 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×5 0.8147 1.8116 0.3810 3.6535 3.1618

By default, unifrnd generates an array that is the same size as a and b after any necessary scalar expansion so that all scalars are expanded to match the dimensions of the other inputs.

If you specify array dimensions sz1. szN , they must match the dimensions of a and b after any necessary scalar expansion.

Generate a 2-by-3 array of random numbers from the continuous uniform distribution with the lower parameter 0 and upper parameter 1 .

sz = [2 3]; r2 = unifrnd(0,1,sz)
r2 = 2×3 0.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×6 0.9706 1.9572 2.4854 3.8003 4.1419 5.4218

Input Arguments

a — Lower endpoint
scalar value | array of scalar values

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

To generate random numbers from multiple distributions, specify a and b using arrays. If both a and b are arrays, then the array sizes must be the same. If either a or b is a scalar, then unifrnd expands the scalar argument into a constant array of the same size as the other argument. Each element in r is the random number generated from the distribution specified by the corresponding elements in a and b .

Example: [0 -1 7 9]

Data Types: single | double

b — Upper endpoint
scalar value | array of scalar values

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

To generate random numbers from multiple distributions, specify a and b using arrays. If both a and b are arrays, then the array sizes must be the same. If either a or b is a scalar, then unifrnd expands the scalar argument into a constant array of the same size as the other argument. Each element in r is the random number generated from the distribution specified by the corresponding elements in a and b .

Example: [1 1 10 10]

Data Types: single | double

sz1. szN — Size of each dimension (as separate arguments)
integers

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

If either a or b is an array, then the specified dimensions sz1. szN must match the common dimensions of a and b after any necessary scalar expansion. The default values of sz1. szN are the common dimensions.

Example: 2,3

Data Types: single | double

sz — Size of each dimension (as a row vector)
row vector of integers

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

If either a or b is an array, then the specified dimensions sz must match the common dimensions of a and b after any necessary scalar expansion. The default values of sz are the common dimensions.

Example: [2 3]

Data Types: single | double

Output Arguments

r — Uniform random numbers
scalar value | array of scalar values

Uniform 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 elements in a and b .

Alternative Functionality

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

The generated code can return a different sequence of numbers from the sequence returned by MATLAB ® if either of the following is true:

GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox) .

Version History

Introduced before R2006a