Random Source
Generate randomly distributed values
Libraries:
DSP System Toolbox /
Sources
Description
The Random Source block outputs a random signal with a uniform or Gaussian (normal) pseudorandom distribution. The block uses the Mersenne Twister random number generator to generate the sequence. The settings in the block dialog box determine the size, data type, and complexity of the signal.
The block can generate a single-channel or a multichannel signal.
Examples
Generate Random Signal with Uniform Distribution
Open the GenerateUniformSignal.slx
model. The Random Source block in the model has the Distribution parameter set to Uniform
. The minimum and maximum values of the uniform distribution are 1 and 3, respectively.
Set the Samples per frame parameter to 512. The output random signal contains 512 samples in each channel (frame). Set the Sample time parameter to 0.1. Setting the Signal complexity to Real
generates a real signal. Since the Initial seed parameter in the block dialog box is set to 1, the random signal that the block outputs is repeatable; that is, each time you simulate the model, the block generates the same pseudorandom sequence.
Run the model. The Random Source block generates a real single-channel signal with a uniform distribution. View the signal in the time scope.
The frame rate of the block MTs
is . View the frame rate in the timing legend.
Change the Sample time to 0.2 seconds and rerun the model. The output frame rate of the block is now .
Generate Random Signal with Gaussian Distribution
Open the GenerateGaussianSignal.slx
model. The Random Source block in the model has the Distribution parameter set to Gaussian
.
Generate Single-Channel Signal
Set the Mean and Variance parameters to 0 and 0.5, respectively. Because these values are scalars, the block generates a single-channel random signal. With the Samples per frame parameter set to 512, the block generates a 512-by-1 signal.
Generate Multichannel Signal
Set the Mean or Variance parameter to a row vector of four elements to generate a random signal with four channels. The value of the Mean parameter in this model is [0 2 4 6]. The Variance parameter must be a scalar or a row vector of four elements to match the dimensions of the Mean parameter. The value of the Variance parameter in this model is 0.5.
Run the model. The block generates a multichannel Gaussian random signal of size 512-by-4.
Repeat Random Signal Output
Use the Repeatability parameter to repeat the block output.
Open the RepeatRandomSignal.slx
model. Set Repeatability to 'Specify Seed'
to produce the same pseudorandom sequence each time you simulate the model. Specify the random seed as 123 through the Initial seed parameter.
Run the model.
Repeat the simulation and notice that the output values are exactly the same as the previous run.
2.3929 2.9615 1.8771 1.5723 2.3697 1.1194 1.4537 1.9619 1.7961 2.1026 1.7842 2.4760 2.4389 1.6864 1.3650 1.8462 2.4581 1.3509
Change the seed to 456 and run the model. The output values change but are exactly the same in subsequent simulations.
1.4975 2.7714 2.1514 1.3261 2.5182 1.2922 2.5673 1.3622 2.3732 2.6170 1.3003 1.9376 2.2513 1.8714 2.1400 2.2082 1.7705 2.2914
Set Repeatability to 'Not repeatable'
and run the model.
1.1354 2.6698 2.1185 2.0068 1.3143 1.6524 2.4179 1.6153 1.5030 2.4327 1.9889 1.1016 2.6093 1.8927 1.4261 1.2325 1.0864 2.2237
Repeat the simulation. The output signal is now different compared to the previous run.
1.1801 1.0135 2.6225 1.4634 1.8974 1.1402 2.9918 1.0033 1.6790 1.1409 2.4337 1.9416 1.0579 2.7062 2.4268 2.2496 1.5049 2.0864
Generate Complex Random Signal
Generate a complex random signal by setting the Signal complexity parameter to Complex
.
Open the ComplexRandomSignal
model. The Distribution parameter in the Random Source block is set to Uniform
. The minimum and maximum values of the uniform distribution are 1 and 3, respectively.
Run the model. The block generates a complex scalar random signal with a uniform distribution. The Complex to Real-Imag block splits the signal into real and imaginary components. Plot the data using the XY Graph block.
Change the distribution to Gaussian
. Set the Mean parameter to a complex number. The real component of the Mean parameter specifies the mean of the real components in the channel, while the imaginary component specifies the mean of the imaginary components in the channel. If you omit the real or imaginary component from the Mean parameter, the block uses a default value of 0 as the mean of that component.
The variance must always be real and positive.
Inherit Signal Properties Via Output Port
Select the Inherit via output port parameter to inherit the properties of the random signal through back propagation.
Open the InheritSignalProperties
model. The model adds the outputs of the Random Source block and the Sine Wave block, and plots the resulting noisy sinusoidal signal in the time scope.
The Random Source block generates a random signal with a Gaussian distribution. The mean and variance of the distribution is 0 and 0.01, respectively.
Select the Inherit via output port parameter in the Random Source block. The block inherits the signal properties such as the size, data type, complexity, and sample time from the Sine Wave block via back propagation. The Sine Wave block generates a complex sinusoidal signal with a sample time of 0.001 seconds. The Random Source block inherits these properties and generates a random signal with the same properties.
Run the model. View the real and imaginary components of the noisy sinusoidal signal in the time scope.
Estimate Frequency-Domain Transfer Function of System Using Random Excitation Input
This example shows how to use the Discrete Transfer Function Estimator block to estimate the frequency-domain transfer function of a system.
Open the ex_discretetransferfunctionestimator.slx
model. The Random Source block generates the system input signal that you pass through the x port of the Discrete Transfer Function Estimator block. The sample rate of the input signal is 44.1 KHz. This input signal passes through a lowpass filter with a normalized cutoff frequency of 0.3. The filtered signal represents the system output signal that you input through the port y of the Discrete Transfer Function Estimator block. Because the Discrete Transfer Function Estimator block outputs complex values, compute the magnitude of the output signal to generate a plot of the transfer function estimate.
Run the model. The array plot shows the system transfer function, a lowpass filter that matches the frequency response of the Discrete FIR Filter block.
Extended Examples
Compute Moving Variance of Noisy Square Wave Signal
Compare the sliding window variance method and the exponentially weighted variance method in Simulink® using the Moving Variance block.
Analyze a Subband of Input Frequencies Using Zoom FFT
Compute the zoom FFT of a sine wave in Simulink.
Adapt Multiple Filters Using LMS Update Block
Adapt multiple filters independently using the same LMS Update block. Use the Adapt port of the LMS Update block to selectively enable or disable the filters from being adapted.
Estimate Data Series Using Forward Linear Predictor
Use the Autocorrelation LPC block to estimate the future values of signal.
Ports
Output
Port_1 — Signal of randomly distributed values
scalar | vector | matrix
Signal of random values with uniform or Gaussian (normal) distribution. The settings in the block dialog box determine the size, data type, and complexity of the signal.
The block can generate a single-channel or a multichannel signal. The Samples per frame parameter determines the number of samples in each channel (column) of the signal. The Output data type and Signal complexity parameters determine the data type and complexity of the signal.
Data Types: single
| double
Complex Number Support: Yes
Parameters
Distribution — Uniform or Gaussian
Uniform
(default) | Gaussian
Specify the distribution from which to draw the random values as one of these:
Uniform
–– The block draws the output samples from a uniform distribution. You can specify the minimum and maximum values of the distribution using the Minimum and Maximum parameters, respectively. You can generate a complex number from the uniform distribution by setting the Signal complexity parameter toComplex
.All values in this [Minimum Maximum] range have an equal likelihood to be selected.
For an example, see Generate Random Signal with Uniform Distribution.
Gaussian
–– The block produces Gaussian random values by using the Ziggurat method. You can specify the mean and variance of the distribution using the Mean and Variance parameters. You can generate a complex Gaussian signal by setting the Signal complexity parameter toComplex
.For an example, see Generate Random Signal with Gaussian Distribution.
Minimum — Minimum value of uniform distribution
0
(default) | scalar | row vector
Specify the minimum value in the uniform distribution as a scalar or a row vector of length N. When you specify a row vector, the block generates an M-by-N matrix containing a unique random distribution in each channel. M is the value you specify in the Samples per frame parameter.
For example, when you set Minimum to [0 0 -3
-3]
and Maximum to [10 10 20
20]
, the block generates a four-channel output whose first and
second columns contain random values in the range [0, 10], and whose third
and fourth columns contain random values in the range [-3, 20].
The Minimum and Maximum values must be scalars or have the same number of columns. When you specify only one of the Minimum and Maximum parameters as a vector, the block expands the other parameter so that it is the same length as the vector.
Tunable: Yes
Dependencies
To enable this parameter, set Distribution to
Uniform
.
Maximum — Maximum value of uniform distribution
1
(default) | scalar | row vector
Specify the maximum value in the uniform distribution as a scalar or a row vector of length N. When you specify a row vector, the block generates an M-by-N matrix containing a unique random distribution in each channel. M is the value you specify in the Samples per frame parameter.
For example, when you set Minimum to [0 0 -3 -3]
and Maximum to [10 10 20 20]
, the
block generates a four-channel output whose first and second columns contain
random values in the range [0, 10], and whose third and fourth columns
contain random values in the range [−3, 20].
The Minimum and Maximum values must be scalars or have the same number of columns. When you specify only one of the Minimum and Maximum parameters as a vector, the block expands the other parameter so it is the same length as the vector.
Tunable: Yes
Dependencies
To enable this parameter, set Distribution to
Uniform
.
Mean — Mean value of Gaussian distribution
0
(default) | scalar | row vector
Specify the mean of the Gaussian (normal) distribution as a scalar or a row vector of length N. When you specify a row vector, the block generates an M-by-N matrix containing a distinct random distribution in each channel. M is the value you specify in the Samples per frame parameter.
The Mean and Variance values must be scalars or have the same number of columns. When you specify only one of the Mean and Variance parameters as a vector, the block expands the other parameter so that it is the same length as the vector.
To generate a complex output signal with a Gaussian
distribution, set Signal complexity to
Complex
and specify a complex value in the
Mean parameter. For more information, see the
Signal complexity parameter description.
Tunable: Yes
Dependencies
To enable this parameter, set Distribution to
Gaussian
.
Complex Number Support: Yes
Variance — Variance of Gaussian distribution
1
(default) | scalar | row vector
Specify the variance of the Gaussian (normal) distribution as a scalar or a row vector of length N. When you specify a row vector, the block generates an M-by-N matrix containing a distinct random distribution in each channel. M is the value you specify in the Samples per frame parameter.
The Mean and Variance values must be scalars or have the same number of columns. When you specify only one of the Mean and Variance parameters as a vector, the block expands the other parameter so that it is the same length as the vector.
To generate a complex output signal with a Gaussian
distribution, the Variance parameter
σ2 specifies the total variance for each
output channel. This value is the sum of the variances of the real and
imaginary components in that channel. For more information, see the
Signal complexity parameter description.
Dependencies
To enable this parameter, set Distribution to
Gaussian
.
Repeatability — Option to repeat block output
Specify seed
(default) | Not repeatable
Option to repeat block output, specified as one of these:
Specify seed
— The block uses the initial seed that you specify in the Initial seed parameter to produce a repeatable output for every simulation.Not repeatable
— The block randomly selects an initial seed and produces a different pseudorandom sequence for every simulation.
For an example that shows the behavior of this parameter, see Repeat Random Signal Output.
Initial seed — Initial seed for random number generator
1
(default) | scalar
Specify the initial seed(s) for the random number generator as a scalar. The generator produces an identical sequence of pseudorandom numbers each time you simulate the block with a particular initial seed.
Dependencies
To enable this parameter, set Repeatability to
Specify seed
.
Inherit via output port — Inherit output port parameters from downstream block
off
(default) | on
When you select this check box, the block inherits the signal properties such as the samples per frame, output data type, signal complexity, and sample time from the downstream block and disables the corresponding parameters in the block dialog box. For an example, see Inherit Signal Properties Via Output Port.
Suppose that you want to back propagate a 1-D vector. The output of the
Random Source block is a 1-D vector of length
M, where the block inherits the length
M from the downstream block. When the
Minimum, Maximum,
Mean, or Variance parameter
specifies N channels, the 1-D vector output contains
M/N samples from each channel. An
error occurs in this case when M is not an integer
multiple of N
.
Suppose that you want to back propagate a M-by-N
signal. When N > 1
, your signal has
N channels. When N =
1
, your signal has M channels. The
value of the Minimum, Maximum,
Mean, or Variance parameter
can be a scalar or a vector of length equal to the number of channels. You
can specify these parameters as row or column vectors, except when the
signal is a row vector. In this case, you must specify the
Minimum, Maximum,
Mean, or Variance parameter as
a row vector.
Sample time — Output sample period
1
(default) | −1 | real positive scalar
Specify the sample period Ts of the random
output sequence as a real positive scalar or -1
. The
output frame period is MTs.
If Ts = −1, the block inherits the output sample period from its output port, but determines the dimensions and complexity of the signal from the block settings.
Dependencies
To enable this parameter, clear the Inherit via output port parameter.
Samples per frame — Samples per output frame
1
(default) | positive integer
Specify the number of samples M in each output frame as a positive integer. The output frame period is MTs, where Ts is the value you specify in the Sample time parameter.
Dependencies
To enable this parameter, clear the Inherit via output port parameter.
Signal complexity — Complexity of output
Real
(default) | Complex
Specify the complexity of the output as Real
or
Complex
. These settings control all channels
of the output. The real and complex components of the output are
statistically independent.
For a complex output with a Uniform
distribution, the block draws the real and imaginary components in each
channel from the same uniform random distribution, defined by the
Minimum and Maximum parameters
for that channel.
For a complex output with a Gaussian
distribution, the block draws the real and imaginary components in each
channel from normal distributions with different means. In this case, the
Mean parameter for each channel must be a complex
value. The real component of the Mean parameter
specifies the mean of the real components in the channel, while the
imaginary component specifies the mean of the imaginary components in the
channel. When you omit the real or imaginary component from the
Mean parameter, the block uses a default value of 0
for the mean of that component.
For example, a Mean parameter setting of
[5+2i 0.5 3i]
generates a three-channel output with
these mean values.
Channel 1 mean | real = 5 | imaginary = 2 |
Channel 2 mean | real = 0.5 | imaginary = 0 |
Channel 3 mean | real = 0 | imaginary = 3 |
For a more detailed example, see Generate Complex Random Signal.
For a complex output, the Variance parameter σ2 specifies the total variance for each output channel. This value is the sum of the variances of the real and imaginary components in that channel.
The block divides the variance value you specify equally between the real and imaginary components.
Dependencies
To enable this parameter, clear the Inherit via output port parameter.
Output data type — Output data type
double
(default) | single
Specify the data type of the output as double precision or single precision.
Dependencies
To enable this parameter, clear the Inherit via output port parameter.
Simulate using — Type of simulation to run
Interpreted execution
(default) | Code generation
Specify the type of simulation to run. You can set this parameter to:
Interpreted execution
–– Simulate model using the MATLAB® interpreter. This option shortens startup time.Code generation
–– Simulate model using generated C code. The first time you run a simulation, Simulink generates C code for the block. The C code is reused for subsequent simulations as long as the model does not change. This option requires additional startup time but provides faster subsequent simulations.
Block Characteristics
Data Types |
|
Direct Feedthrough |
|
Multidimensional Signals |
|
Variable-Size Signals |
|
Zero-Crossing Detection |
|
More About
Uniform Distribution
Uniform distribution is a type of probability distribution in which all outcomes are equally likely.
Scalar Random Variable
Consider a scalar random variable X that the block draws from a uniform distribution U:
X ~ U([A,B]), where A and B are the minimum and maximum values of the distribution, respectively.
Alternatively, you can write X in terms of a scaled normalized uniform variable:
Uniform Random Vector
When you configure the block to have N > 1 channels, each output sample is a random vector of N independent entries. You can write X as , where Xk ~ U([Ak,Bk]) with {A1,…, AN} and {B1,…, BN} are the minimum and the maximum values of the vector parameters, respectively. When you set Samples per frame M > 1, each output frame is an M-by-N matrix of M independent uniform random row vectors.
Complex Uniform Random Variable
When you set the block to generate a complex scalar random variable, X := Y + jZ, where Y, Z ~ U([A, B]).
Alternatively, you can write X as
Complex Uniform Random Vector
When X is a vector of complex random variables, , where Yk, Zk ~ U([Ak, Bk]) are all independent.
Gaussian Distribution
Scalar Random Variable
When you set the block to generate a random variable from a Gaussian (or normal) distribution, the variable X ~ N(μ,σ2), where μ and σ2 are the mean and variance values, respectively.
Alternatively, you can write X as is a standard normal variable.
Gaussian Random Vector
When you set the block to have N > 1 channels, each output sample is a random vector of N independent entries. When X is a Gaussian random vector, , where Xk ~ N(μk, σk2), {μ1, …, μN} and {σ12, …, σN2} are the mean and variance values of N channels, respectively. The covariance matrix of the vector X is a diagonal matrix given by .
Complex Gaussian Random Variable
When you set the block to generate a complex scalar random variable, X
:= Y + jZ, where
Y ~ N(Re
(μ),(σ2/2))
and Z ~ N(Im
(μ),(σ2/2))
are independent. The mean value can be complex.
Alternatively, you can write X as .
Complex Gaussian Random Vector
When X is a vector of complex random variables, , where Yk ~ N(Re
(μk),
(σk2/2))
and Zk ~ N(Im
(μk),
(σk2/2))
with {μ1, …,
μN} and
{σ12, …,
σN2}
are the mean and variance values of N channels,
respectively.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.
Version History
Introduced before R2006aR2024a: Random Source block has been updated
The Random Source block has been updated with these changes:
Support for Mersenne Twister random number generator.
The
Gaussian
distribution always uses the Zigguart method. TheSum of uniform values
method is no longer supported.Repeatable
option is removed. To generate a repeatable output, set Repeatability toSpecify seed
.The initial seed must be a scalar. You can no longer specify a vector of seed values.
When you specify an initial seed, the seed is no longer tunable, that is, you cannot change the value of the seed during simulation.
The Sample mode parameter is removed. The sample mode of the block is now always discrete.
If you open a model saved in R2023b or a previous version of MATLAB, the model continues to use the older version of the block in R2024a. To use the updated version, obtain the Random Source block from the DSP System Toolbox/Sources library.
See Also
Blocks
- Discrete Impulse | Maximum | Minimum | Signal From Workspace | Standard Deviation | Variance | Constant (Simulink) | Random Number (Simulink) | Signal Generator (Simulink)
Functions
rand
|randn
|RandStream
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.
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
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
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
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)