Uniformly distributed pseudorandom integers - MATLAB randi - MathWorks (2024)

Table of Contents
Syntax Description Examples Square Matrix of Random Integers Random Integers Within Specified Interval Control Random Number Generation 3-D Array of Random Integers Random Integers of Other Data Types Size Defined by Existing Array Size and Numeric Data Type Defined by Existing Array Random Complex Integers Random Logical Array Input Arguments imax — Largest integer in sample interval positive integer imin — Smallest integer in sample interval 1 (default) | scalar integer n — Size of square matrix integer value sz1,...,szN — Size of each dimension (as separate arguments) integer values sz — Size of each dimension (as a row vector) integer values typename — Data type (class) to create "double" (default) | "single" | "int8" | "uint8" | "int16" | "uint16" | "int32" | "uint32" | "logical" p — Prototype of array to create numeric array | logical array s — Random number stream RandStream object Tips Extended Capabilities C/C++ Code Generation Generate C and C++ code using MATLAB® Coder™. Thread-Based Environment Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool. GPU Arrays Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™. Distributed Arrays Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™. Version History R2023a: Create random logical array R2022a: Match complexity with "like", and use "like" with RandStream object R2014a: Match data type of an existing variable with 'like' R2013b: Non-integer size inputs are not supported See Also Topics MATLAB Command Americas Europe Asia Pacific FAQs References

Uniformly distributed pseudorandom integers

collapse all in page

Syntax

X = randi(imax)

X = randi(imax,n)

X = randi(imax,sz1,...,szN)

X = randi(imax,sz)

X = randi(___,typename)

X = randi(___,"like",p)

X = randi([imin,imax],___)

X = randi(s,___)

Description

X = randi(imax) returns a pseudorandom scalar integer between 1 and imax.

example

X = randi(imax,n) returns an n-by-n matrix of pseudorandom integers drawn from the discrete uniform distribution on the interval [1,imax].

X = randi(imax,sz1,...,szN) returns an sz1-by-...-by-szN array where sz1,...,szN indicate the size of each dimension. For example, randi(10,3,4) returns a 3-by-4 array of pseudorandom integers between 1 and 10.

example

X = randi(imax,sz) returns an array where size vector sz defines size(X). For example, randi(10,[3 4]) returns a 3-by-4 array of pseudorandom integers between 1 and 10.

example

X = randi(___,typename) returns an array of pseudorandom integers between 1 and imax of data type typename. The typename input can be "single", "double", "int8", "uint8", "int16", "uint16", "int32", "uint32", or "logical". You can use any of the input arguments in the previous syntaxes.

example

X = randi(___,"like",p) returns an array of pseudorandom integers like p; that is, with the same data type and complexity (real or complex) as p. You can specify either typename or "like", but not both.

example

X = randi([imin,imax],___) returns an array containing integers drawn from the discrete uniform distribution on the interval [imin,imax], using any of the above syntaxes.

X = randi(s,___) generates integers from random number stream s instead of the default global stream. To create a stream, use RandStream. You can specify s followed by any of the input argument combinations in previous syntaxes.

Examples

collapse all

Square Matrix of Random Integers

Open Live Script

Generate a 5-by-5 matrix of random integers between 1 and 10. The first input to randi indicates the largest integer in the sampling interval (the smallest integer in the interval is 1).

r = randi(10,5)
r = 5×5 9 1 2 2 7 10 3 10 5 1 2 6 10 10 9 10 10 5 8 10 7 10 9 10 7

Random Integers Within Specified Interval

Open Live Script

Generate a 10-by-1 column vector of uniformly distributed random integers from the sample interval [-5,5].

r = randi([-5,5],10,1)
r = 10×1 3 4 -4 5 1 -4 -2 1 5 5

Control Random Number Generation

Open Live Script

Save the current state of the random number generator and create a 1-by-5 vector of random integers.

s = rng;r = randi(10,1,5)
r = 1×5 9 10 2 10 7

Restore the state of the random number generator to s, and then create a new 1-by-5 vector of random integers. The values are the same as before.

r1 = 1×5 9 10 2 10 7

3-D Array of Random Integers

Open Live Script

Create a 3-by-2-by-3 array of uniformly distributed random integers between 1 and 500.

X = randi(500,[3,2,3])
X = X(:,:,1) = 408 457 453 317 64 49X(:,:,2) = 140 483 274 79 479 486X(:,:,3) = 479 71 243 211 401 458

Random Integers of Other Data Types

Open Live Script

Create a 1-by-4 vector of random numbers between 1 and 100 whose elements are of type int16.

r = randi(100,1,4,"int16")
r = 1x4 int16 row vector 82 91 13 92
class(r)
ans = 'int16'

Size Defined by Existing Array

Open Live Script

Create a matrix of uniformly distributed random integers between 1 and 10 with the same size as an existing array.

A = [3 2; -2 1];sz = size(A);X = randi(10,sz)
X = 2×2 9 2 10 10

It is a common pattern to combine the previous two lines of code into a single line:

X = randi(10,size(A));

Size and Numeric Data Type Defined by Existing Array

Open Live Script

Create a 2-by-2 matrix of 8-bit signed integers.

p = int8([3 2; -2 1]);

Create an array of random integers that is the same size and data type as p.

X = randi(10,size(p),"like",p)
X = 2x2 int8 matrix 9 2 10 10
class(X)
ans = 'int8'

Random Complex Integers

Since R2022a

Open Live Script

Generate 10 random complex integers from the discrete uniform distribution over a square domain with real and imaginary parts in the interval [-5,5].

a = randi([-5,5],10,1,"like",1i)
a = 10×1 complex 3.0000 + 4.0000i -4.0000 + 5.0000i 1.0000 - 4.0000i -2.0000 + 1.0000i 5.0000 + 5.0000i -4.0000 + 5.0000i 5.0000 + 0.0000i 3.0000 - 4.0000i -1.0000 + 5.0000i 3.0000 + 5.0000i

Random Logical Array

Since R2023a

Open Live Script

Create a 5-by-5 matrix of random logical values (0s and 1s) with a discrete uniform distribution.

r = randi([0 1],5,"logical")
r = 5x5 logical array 1 0 0 0 1 1 0 1 0 0 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1

Input Arguments

collapse all

imaxLargest integer in sample interval
positive integer

Largest integer in sample interval, specified as a positiveinteger. randi draws values from the uniform distributionin the sample interval [1,imax].

Example: randi(10,5)

iminSmallest integer in sample interval
1 (default) | scalar integer

Smallest integer in sample interval, specified as a scalar integer.

Both imin and imax mustbe integers that satisfy iminimax.

For example, randi([50,100],5) returns a5-by-5 matrix of random integers between (and including) 50 and 100.

nSize of square matrix
integer value

Size of square matrix, specified as an integer value.

  • If n is 0, then X isan empty matrix.

  • If n is negative, then it is treatedas 0.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

sz1,...,szNSize of each dimension (as separate arguments)
integer values

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

  • If the size of any dimension is 0,then X is an empty array.

  • If the size of any dimension is negative, then itis treated as 0.

  • Beyond the second dimension, randi ignorestrailing dimensions with a size of 1. For example, randi([5,10],3,1,1,1) producesa 3-by-1 vector of random integers between 5 and 10.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

szSize of each dimension (as a row vector)
integer values

Size of each dimension, specified as a row vector of integervalues. Each element of this vector indicates the size of the correspondingdimension:

  • If the size of any dimension is 0,then X is an empty array.

  • If the size of any dimension is negative, then itis treated as 0.

  • Beyond the second dimension, randi ignores trailing dimensions with a size of 1. For example, randi([5,10],[3 1 1 1]) produces a 3-by-1 vector of random integers between 5 and 10.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

typenameData type (class) to create
"double" (default) | "single" | "int8" | "uint8" | "int16" | "uint16" | "int32" | "uint32" | "logical"

Data type (class) to create, specified as "double", "single", "int8", "uint8", "int16", "uint16", "int32", "uint32", "logical", or the name of another class that provides randi support.

Example: randi(5,5,"int8")

pPrototype of array to create
numeric array | logical array

Prototype of array to create, specified as a numeric or logical array.

Example: randi(5,5,"like",p)

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | logical
Complex Number Support: Yes

sRandom number stream
RandStream object

Random number stream, specified as a RandStream object.

Example: s = RandStream("dsfmt19937"); randi(s,[5,10],[3 1])

Tips

  • The sequence of numbers produced by randi isdetermined by the internal settings of the uniform pseudorandom numbergenerator that underlies rand, randi,and randn. You can control that shared random numbergenerator using rng.

  • The arrays returned by randi can contain repeated integer values. This behavior is sometimes referred to as sampling with replacement. Use randperm if you require all unique values.

  • If imin and imax are outside the range of the output type (as specified by typename or by the prototype p), then randi first creates random integers within the interval [imin,imax] and converts any resulting out-of-range integers to the minimum or maximum value of the output type. For example:

    rng default;r = randi([-10 10],1,10)
    r = 7 9 -8 9 3 -8 -5 1 10 10
    rng default;r = randi([-10 10],1,10,"logical")
    r = 1×10 logical array 1 1 0 1 1 0 0 1 1 1

Extended Capabilities

Version History

Introduced in R2008b

expand all

You can create a random logical array by specifying typename as "logical" or the prototype p as a logical array. For example, see Random Logical Array.

Specifying a dimension that is not an integer causes an error. Use floor to convert non-integer size inputs to integers.

See Also

rand | randn | rng | RandStream | randperm

Topics

  • Random Integers
  • Create Arrays of Random Numbers
  • Generate Random Numbers That Are Repeatable
  • Generate Random Numbers That Are Different
  • Creating and Controlling a Random Number Stream
  • Class Support for Array-Creation Functions
  • Why Do Random Numbers Repeat After Startup?

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.

Uniformly distributed pseudorandom integers - MATLAB randi- MathWorks (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

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Uniformly distributed pseudorandom integers - MATLAB randi
- MathWorks (2024)

FAQs

How to do randi in Matlab? ›

cR = randi( r , n , codist ) creates an n -by- n codistributed matrix of uniformly distributed random integers in the range defined by r and uses codist to specify the distribution of the array values across the workers. If r is a scalar, the function creates random integers in the range 1 to r .

How do you find the uniform distribution in Matlab? ›

X = rand returns a random scalar drawn from the uniform distribution in the interval (0,1). 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.

How do you create a uniform random variable 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.

How to use randn in Matlab? ›

X = randn( sz ) returns an array of random numbers where size vector sz defines size(X) . For example, randn([3 4]) returns a 3-by-4 matrix. X = randn(___, typename ) returns an array of random numbers of data type typename . The typename input can be either "single" or "double" .

What is the difference between Randperm and Randi? ›

Unlike randi, which can give an array containing repeated values, the array returned by randperm has no repeated values. Successive calls to any of above functions return different results. This behavior is helpful for creating several different arrays of random values.

What is the difference between Rand and Randi? ›

Details. rand() , randn() , randi() create random matrices of size n x m , where the default is square matrices if m is missing. rand() uses the uniform distribution on ]0, 1[ , while randn() uses the normal distribution with mean 0 and standard deviation 1. randi() generates integers between imax[1] and imax[2] resp.

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 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 you code a uniform 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. For this example, X ~ U(0, 23) and f(x) = 123−0 for 0 ≤ X ≤ 23.

What is the formula for the uniform distribution? ›

If X has a uniform distribution where a < x < b or a ≤ x ≤ b, then X takes on values between a and b (may include a and b). All values x are equally likely. We write X ∼ U(a, b). The mean of X is μ=a+b2 μ = a + b 2 .

How do you find the uniform distribution of a random variable? ›

To find the probability (area) under the uniform distribution, use the following formulas.
  1. P(X≥x)=P(X>x)=(1b−a)⋅(b−x)
  2. P(X≤x)=P(X<x)=(1b−a)⋅(x−a)
  3. P(x1≤X≤x2)=P(x1<X<x2)=(1b−a)⋅(x2−x1)
Mar 12, 2023

How to plot a uniform distribution? ›

Graph of uniform distribution is a rectangle. Rectangle begins at the minimum data value of a and ends at the maximum data value of b. The horizontal axis represents values of the random variable, X. The vertical axis represents values of the probability density function, f(x).

What is Randi in MATLAB? ›

The randi function returns double integer values drawn from a discrete uniform distribution. For example, create a 1000-by-1 column vector containing integer values drawn from a discrete uniform distribution. r2 = randi(10,1000,1); All the values in r2 are in the close interval [1, 10].

How do you generate normally distributed random numbers in MATLAB? ›

r = normrnd( mu , sigma , sz1,...,szN ) generates an array of normal random numbers, where sz1,...,szN indicates the size of each dimension. r = normrnd( mu , sigma , sz ) generates an array of normal random numbers, where vector sz specifies size(r) .

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

In summary, randn generates numbers from a normal distribution with a mean of 0 and a standard deviation of 1, while `rand` generates numbers from a uniform distribution between 0 and 1.

How to do random sampling in MATLAB? ›

y = randsample( population , k ) returns a vector of k values sampled uniformly at random, without replacement, from the values in the vector population . y = randsample(___, replacement ) returns a sample taken with replacement if replacement is true , or without replacement if replacement is false .

How to generate random numbers in MATLAB? ›

Create Arrays of Random Numbers
  1. rng("default") r1 = rand(1000,1); All the values in r1 are in the open interval (0,1). ...
  2. r2 = randi(10,1000,1); All the values in r2 are in the close interval [1, 10]. ...
  3. r3 = randn(1000,1); ...
  4. r4 = randperm(15,5);

How to do backslash in MATLAB? ›

MATLAB has a special operator called backslash for solving this type of system, and it usually gives excellent results. It is the method people use in professional settings. To use it, store A and b and type A\b . You may know two other methods in MATLAB for solving the matrix equation .

How to calculate bias in MATLAB? ›

To find the bias, call the second element of vector x . Create a numerictype object with slope bias scaling. Create a fi object with numerictype T . Verify that the fi object that you created has the correct specifications by finding the range of a .

References

Top Articles
Latest Posts
Article information

Author: Arielle Torp

Last Updated:

Views: 6776

Rating: 4 / 5 (61 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Arielle Torp

Birthday: 1997-09-20

Address: 87313 Erdman Vista, North Dustinborough, WA 37563

Phone: +97216742823598

Job: Central Technology Officer

Hobby: Taekwondo, Macrame, Foreign language learning, Kite flying, Cooking, Skiing, Computer programming

Introduction: My name is Arielle Torp, I am a comfortable, kind, zealous, lovely, jolly, colorful, adventurous person who loves writing and wants to share my knowledge and understanding with you.