Continuous uniform random numbers - MATLAB unifrnd (2024)

Continuous uniform random numbers

collapse all in page

Syntax

r = unifrnd(a,b)

r = unifrnd(a,b,sz1,...,szN)

r = unifrnd(a,b,sz)

Description

example

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

example

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

example

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

Examples

collapse all

Generate Uniform Random Number

Open Live Script

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

r = unifrnd(0,1)

Generate Uniform Random Numbers

Open Live Script

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

collapse all

aLower 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

bUpper 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,...,szNSize 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.

  • 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, unifrnd ignores 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

szSize 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.

  • 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, unifrnd ignores 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

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

  • unifrnd is a function specific to the continuous uniform distribution. Statistics and Machine Learning Toolbox™ also offers the generic function random, which supports various probability distributions. To use random, create a UniformDistribution probability distribution object and pass the object as an input argument or specify the probability distribution name and its parameters. Note that the distribution-specific function unifrnd is faster than the generic function random.

  • Use rand to generate numbers from the uniform distribution on the interval (0,1).

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

Extended Capabilities

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

Version History

Introduced before R2006a

See Also

UniformDistribution | rand | random | unifpdf | unifcdf | unifinv | unifstat | unifit

Topics

  • Generate Random Numbers Using Uniform Distribution Inversion
  • Uniform Distribution (Continuous)

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Continuous uniform random numbers - MATLAB unifrnd (1)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

Contact your local office

Continuous uniform random numbers - MATLAB unifrnd (2024)

FAQs

How do you generate random numbers uniform distribution in MATLAB? ›

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.

What is the difference between Unifrnd and Rand? ›

unifrnd is part of the Statistics Toolbox, whereas rand is a Matlab basic function. unifrand is just a wrapper of rand that lets you specify additional parameters to define the interval of the random values ( rand outputs values in [0,1]). You could do the same with rand using (a-b)*rand(...)+

Does a continuous random variable have a uniform distribution? ›

The uniform distribution is a continuous probability distribution and is concerned with events that are equally likely to occur. The continuous random variable X is said to be uniformly distributed or have a rectangular distribution in the interval [a,b].

How to generate random numbers for other continuous distributions? ›

The inversion method relies on the principle that continuous cumulative distribution functions (cdfs) range uniformly over the open interval (0,1). If u is a uniform random number on (0,1), then x = F - 1 ( u ) generates a random number x from any continuous distribution with the specified cdf F .

How to generate uniform random numbers? ›

The main idea here is we take an integer, square it, and then use the middle part of that integer as our next random integer, repeating the process as many times as we need. To generate the Uniform(0,1) random variable, we would divide each generated integer by the appropriate power of ten.

Which function is used to generate random samples from continuous uniform distribution? ›

The runif() function generates random deviates of the uniform distribution and is written as runif(n, min = 0, max = 1) . We may easily generate a number of n random samples within any interval, defined by the min and the max argument.

What is the difference between uniform distribution and normal distribution? ›

The normal distribution has a peak at the mean and decreases as the values move away from the mean, which means that values near the mean are more likely than values far from the mean. The uniform distribution has no peak and is constant across the range, which means that all values are equally likely.

Why not use rand? ›

The most visible problem of it is that it lacks a distribution engine: rand gives you a number in interval [0 RAND_MAX] . It is uniform in this interval, which means that each number in this interval has the same probability to appear. But most often you need a random number in a specific interval.

What does randn do in Matlab? ›

The randn function generates arrays of random numbers whose elements are normally distributed with mean 0 and variance 1. Y = randn(n) returns an n -by- n matrix of random entries. An error message appears if n is not a scalar. Y = randn(m,n) or Y = randn([m n]) returns an m -by- n matrix of random entries.

What is the difference between discrete and continuous uniform distribution? ›

Uniform distributions are probability distributions with equally likely outcomes. In a discrete uniform distribution, outcomes are discrete and have the same probability. In a continuous uniform distribution, outcomes are continuous and infinite.

What is the CDF of a uniform random variable? ›

Theorem: For any cumulative distribution function F, with inverse function F-1, if U has a Uniform (0,1) distribution, then a random variable G = F-1(U) has F as a cdf.

What is the characteristic of continuous uniform distribution? ›

The continuous uniform distribution is such that the random variable X takes values between a (lower limit) and b (upper limit). In the field of statistics, a and b are known as the parameters of continuous uniform distribution. We cannot have an outcome of either less than a or greater than b .

What are uniformly distributed random numbers? ›

Uniformly distributed random numbers on an interval have equal probability of being selected or happening. Normally distributed random numbers on an interval have probabilities that follow the normal distribution bell curve, so numbers closer to the mean are more likely to be selected or to happen.

How do we display the distribution of a continuous random variable? ›

The probability distribution of a continuous random variable X is an assignment of probabilities to intervals of decimal numbers using a function f(x), called a density function, in the following way: the probability that X assumes a value in the interval [a,b] is equal to the area of the region that is bounded above ...

Is it possible to generate same random numbers everytime? ›

Every time you initialize the generator using the same algorithm and seed, you always get the same result. First, initialize the random number generator to make the results in this example repeatable. For example, the following code sets the seed to 1 and the generator algorithm to Mersenne Twister. rng(1,"twister");

What function would be used to generate random numbers from a uniform distribution? ›

The function uniform() in the np. random module generates random numbers on the interval [ low , high ) from a Uniform distribution. The size kwarg is how many random numbers you wish to generate, and is a kwarg in all of Numpy's random number generators.

How do you draw a random number from a distribution in Matlab? ›

R = random( pd ) returns a random number from the probability distribution object pd . R = random(___, sz1,...,szN ) generates an array of random numbers from the specified probability distribution using input arguments from any of the previous syntaxes, where sz1,...,szN indicates the size of each dimension.

How to generate normally distributed random numbers in matlab? ›

r = normrnd( mu , sigma ) generates a random number from the normal distribution with mean parameter mu and standard deviation parameter sigma . r = normrnd( mu , sigma , sz1,...,szN ) generates an array of normal random numbers, where sz1,...,szN indicates the size of each dimension.

What is the formula for uniform random distribution? ›

The notation for the uniform distribution is: X ~ U(a, b) where a = the lowest value of x and b = the highest value of x. The probability density function is f(x) = 1b−a for a ≤ x ≤ b.

Top Articles
Latest Posts
Article information

Author: Prof. An Powlowski

Last Updated:

Views: 5893

Rating: 4.3 / 5 (44 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Prof. An Powlowski

Birthday: 1992-09-29

Address: Apt. 994 8891 Orval Hill, Brittnyburgh, AZ 41023-0398

Phone: +26417467956738

Job: District Marketing Strategist

Hobby: Embroidery, Bodybuilding, Motor sports, Amateur radio, Wood carving, Whittling, Air sports

Introduction: My name is Prof. An Powlowski, I am a charming, helpful, attractive, good, graceful, thoughtful, vast person who loves writing and wants to share my knowledge and understanding with you.