Main Content

Random Source

Generate randomly distributed values

  • Random Source block with uniform distribution

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

expand all

Open the GenerateUniformSignal.slx model by clicking the Open Model button. 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 $M$ to 512. The output random signal contains 512 samples in each channel (frame). Set the Sample time parameter $Ts$ 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 $512 \times 0.1 = 51.2$. 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 $512 \times 0.2 = 102.4$.

Open the GenerateGaussianSignal.slx model by clicking the Open Model button. 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.

Use the Repeatability parameter to repeat the block output.

Open the RepeatRandomSignal.slx model by clicking the Open Model button. 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.

    2.1434    1.7701    2.3091
    2.7618    2.9590    2.5009
    2.8300    2.5384    2.9067
    1.1375    1.1830    2.8603
    1.9703    2.4747    1.9586
    2.4352    2.9138    2.6938

Repeat the simulation. The output signal is now different compared to the previous run.

    1.5676    1.5625    1.7247
    1.5965    2.3089    1.7538
    2.9536    1.6863    2.4400
    2.0949    2.9578    1.4367
    2.3070    2.6253    1.4087
    2.9438    1.1947    1.8915

Generate a complex random signal by setting the Signal complexity parameter to Complex.

Open the ComplexRandomSignal model by clicking the Open Model button. 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.

Select the Inherit via output port parameter to inherit the properties of the random signal through back propagation.

Open the InheritSignalProperties model by clicking the Open Model button. 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.

This example shows how to use the Discrete Transfer Function Estimator block to estimate the frequency-domain transfer function of a system.

Open the 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.

Ports

Output

expand all

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

expand all

Distribution Settings

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 to Complex.

    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 to Complex.

    For an example, see Generate Random Signal with Gaussian Distribution.

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.

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.

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

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.

Random Generator Settings

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.

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.

Signal Properties

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.

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.

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.

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.

σ2=σRe2+σIm2

The block divides the variance value you specify equally between the real and imaginary components.

σRe2=σ22σIm2=σ22

Dependencies

To enable this parameter, clear the Inherit via output port parameter.

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.

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

double | single

Direct Feedthrough

no

Multidimensional Signals

no

Variable-Size Signals

no

Zero-Crossing Detection

no

More About

expand all

Extended Capabilities

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

Version History

Introduced before R2006a

expand all