Continuous uniform random numbers - MATLAB unifrnd - MathWorks France (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

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)

Commande MATLAB

Vous avez cliqué sur un lien qui correspond à cette commande MATLAB:

 

Pour exécuter la commande, saisissez-la dans la fenêtre de commande de MATLAB. Les navigateurs web ne supportent pas les commandes MATLAB.

Continuous uniform random numbers - MATLAB unifrnd- MathWorks France (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
- MathWorks France (2024)

FAQs

How do you generate a random uniform number in MATLAB? ›

MATLAB has a long list of random number generators. For example, you can use rand() to create a random number in the interval (0,1), X = rand returns a single uniformly distributed random number in the interval (0,1). X = rand(n) returns an n-by-n matrix of random numbers.

What is unifrnd in MATLAB? ›

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

What is uniform distribution in MATLAB? ›

The uniform distribution (also called the rectangular distribution) is a two-parameter family of curves that is notable because it has a constant probability distribution function (pdf) between its two bounding parameters.

What is the random variable code in MATLAB? ›

R = random(___, sz ) generates an array of random numbers from the specified probability distribution using input arguments from any of the previous syntaxes, where vector sz specifies size(r) .

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.

What does random uniform () do? ›

uniform() method in Python is used to return a random floating-point number that is greater than or equal to the specified low boundary, and less than or equal to the specified high boundary.

What is the difference between Unifrnd and Rand in MATLAB? ›

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.

What is the Unidrnd function in MATLAB? ›

unidrnd is a function specific to discrete uniform 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.

What is the Unifcdf function in MATLAB? ›

Description. p = unifcdf( x , a , b ) returns the continuous uniform cumulative distribution function (cdf) at each value in x using the corresponding lower endpoint a and upper endpoint b .

How to plot a uniform random variable in Matlab? ›

X = rand( n ) returns an n -by- n matrix of uniformly distributed random numbers. X = rand( sz1,...,szN ) returns an sz1 -by-... -by- szN array of random numbers where sz1,...,szN indicate the size of each dimension. For example, rand(3,4) returns a 3-by-4 matrix.

How does Matlab generate random numbers? ›

Use the rand , randn , and randi functions to create sequences of pseudorandom numbers, and the randperm function to create a vector of randomly permuted integers. Use the rng function to control the repeatability of your results.

What are uniformly distributed random numbers? ›

In statistics, uniform distribution refers to a type of probability distribution in which all outcomes are equally likely. A deck of cards has within it uniform distributions because the likelihood of drawing a heart, a club, a diamond, or a spade is equally likely.

How do you fix random numbers in MATLAB? ›

Select MATLAB > General, and then select a different option for Default algorithm and select a different value for Default seed in the Random Number Generation preference.

How to generate a random number? ›

Hardware based random-number generators can involve the use of a dice, a coin for flipping, or many other devices. A pseudo-random number generator is an algorithm for generating a sequence of numbers whose properties approximate the properties of sequences of random numbers.

How do you unique random numbers in MATLAB? ›

You can use the randperm function to create a double array of random integer values that have no repeated values. For example, create a 1-by-5 array containing integers randomly selected from the range [1, 15]. r4 = randperm(15,5);

How to generate a random number in a certain range in matlab? ›

Use the rand function to draw the values from a uniform distribution in the open interval, (50,100). a = 50; b = 100; r = (b-a). *rand(1000,1) + a; Verify the values in r are within the specified range.

How do you generate random normal 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.

How do you generate distinct random numbers in Matlab? ›

One way to get different random numbers is to initialize the generator using a different seed every time. Doing so ensures that you don't repeat results from a previous session. Execute the rng("shuffle") command once in your MATLAB session before calling any of the random number functions.

How do computers generate uniform random numbers? ›

However, computers are inherently incapable of being random. Instead, programmers rely on pseudorandom number generators (PRNGs). These are simply a category of algorithms that programmatically generate new random numbers from a given starting value called the seed.

Top Articles
Latest Posts
Article information

Author: Carlyn Walter

Last Updated:

Views: 5655

Rating: 5 / 5 (70 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Carlyn Walter

Birthday: 1996-01-03

Address: Suite 452 40815 Denyse Extensions, Sengermouth, OR 42374

Phone: +8501809515404

Job: Manufacturing Technician

Hobby: Table tennis, Archery, Vacation, Metal detecting, Yo-yoing, Crocheting, Creative writing

Introduction: My name is Carlyn Walter, I am a lively, glamorous, healthy, clean, powerful, calm, combative person who loves writing and wants to share my knowledge and understanding with you.