Random Integer Generator
Generate integers randomly distributed in specified range
Libraries:
Communications Toolbox /
Comm Sources /
Random Data Sources
Description
The Random Integer Generator block generates uniformly distributed random integers in the range [0, M-1], where M is specified by the Set size parameter. Use this block to generate random binary-valued or integer-valued data.
Examples
Random Integer Generator Initial Seed Source
Demonstrate that sequences output from the Random Integer Generator
block differ when you set the initial seed source to Auto
. Show the output sequences repeat when you set the initial seed source to Parameter
and the initial seed value is not changed. Show the output sequence changes when you set the initial seed source to Parameter
and you change the initial seed.
Run the slex_rng_seed_source
model twice and compare the output sequence.
Seed source = Auto 6 1 7 7 1 7 7 3 5 6 0 1 2 3 4 7 7 6 7 7
Change the Initial seed source to Parameter
. Run the slex_rng_seed_source
model twice with the initial seed value set to 0
. Compare the results with previous results.
Seed source = Parameter, Seed = 0 6 1 6 6 7 7 7 7 1 7 1 1 7 3 7 7 5 6 5 5 0 1 0 0 2 3 2 2 4 7 4 4 7 6 7 7 7 7 7 7
Run after changing the initial seed value to 1993
. Compare the results with previous results.
Seed = 1993 6 1 6 6 0 7 7 7 7 1 1 7 1 1 7 7 3 7 7 2 5 6 5 5 1 0 1 0 0 1 2 3 2 2 4 4 7 4 4 5 7 6 7 7 4 7 7 7 7 1
Plot Noisy QPSK Constellation
Apply QPSK modulation to a signal of random data. Pass the modulated signal through an additive white Gaussian noise (AWGN) channel. Plot the signal constellation.
The doc_qpsk_mod
model generates QPSK data, applies the AWGN, and displays the resulting constellation diagram.
Run the model with the Eb/N0 of the AWGN Channel block set to 15 dB. The constellation diagram shows the QPSK symbol samples with AWGN.
Change the Eb/N0 from 15 dB to 10 dB. The noise level increases as shown by the greater distance between the samples in the constellation diagram.
Viterbi Decoding of Binary Symmetric Channel Impaired Data
The cm_ex_viterbi_decode_binary_seq
model generates a binary sequence using the Random Integer Generator
block. The sequence is encoded with the Convolutional Encoder
block and then impaired with the Binary Symmetric Channel
block. The Viterbi Decoder
block decodes the data sequence and the bit error rate is computed.
The InitFcn
callback is used to initialize workspace parameters for samples per frame, BSC error probability, and the Viterbi decoder traceback depth. The signal delay between the transmitted and received signal is equal to the traceback depth. The signal delay is needed for the error rate calculation.
To produce a binary bit stream, the Random Integer Generator
block specifies a set size of 2
, and output type of boolean
.
The computed error rate approximates the Error probability
specified in the Binary Symmetric Channel
block.
Computed error rate = 0.095023
Limitations
To use this block in a For Each Subsystem (Simulink) you must set
Source of initial seed
toAuto
and the model toNormal
orAccelerator
simulation mode. This ensures that each run will generate independent noise samples.
Ports
Output
Out — Random integer output
scalar | vector | matrix
Random integer output, returned as a scalar, vector, or matrix. This port is unnamed on the block. The data type is set using the Output data type parameter.
The number of rows in the output data equals the value of the Samples per frame parameter and corresponds to the number of samples in one frame. The number of columns in the output data equals the number of elements in the Set size parameter and corresponds to the number of channels.
Parameters
To edit block parameters interactively, use the Property Inspector. From the Simulink® Toolstrip, on the Simulation tab, in the Prepare gallery, select Property Inspector.
Set size — Set size
8
(default) | positive integer | row vector of positive integers
Set size,M, specified as a positive integer or row vector of positive integers. The block generates integers in the range [0, (M – 1)]. The number of elements in Set size corresponds to the number of independent channels output from the block.
If Set size is a scalar, then all output random variables are independent and identically distributed (i.i.d.).
If Set size is a vector, then the length of the vector determines the number of output channels. The channels can have differing output ranges.
Source of initial seed — Source of initial seed
Auto
(default) | Parameter
Source of the initial seed for the random number generator, specified as either:
Auto
–– The block uses the global random number stream. If you use theparsim
(Simulink) function, see Tips.Parameter
–– The block sets the random number generator seed to Initial seed.
For more information, see Managing the Global Stream Using RandStream and Random Number Generators.
Initial seed — Initial seed value
0
(default) | nonnegative integer
Initial seed value for the random number generator, specified as a nonnegative integer. If the Initial seed parameter is a constant, then the resulting sequence is repeatable.
Tunable: Yes
Dependencies
To enable this parameter, set the Source of
initial seed parameter to
Parameter
.
Sample time — Sample time
1
(default) | -1 | positive scalar
Positive scalars specify the time in seconds between each
sample of the output signal. If you set the sample time to -1
, the output
signal inherits the sample time from downstream. For information on the relationship between the
Sample time and Samples per frame parameters,
see
Sample Timing.
Samples per frame — Samples per frame
1
(default) | positive integer
Samples per frame, specified as a positive integer indicating the number of samples per frame in one channel of the output data. For information on the relationship between Sample time and Samples per frame, see Sample Timing.
Output data type — Output data type
double
(default) | single
| uint8
| uint16
| uint32
| boolean
Output data type, specified as double
,
single
, uint8
,
uint16
, uint32
, or
boolean
. If this parameter is set to
boolean
, you must set the Set size parameter to
2
.
Simulate using — Type of simulation to run
Interpreted execution
(default) | Code generation
Type of simulation to run, specified as Interpreted
execution
or Code generation
.
Interpreted execution
— Simulate the model by using the MATLAB® interpreter. This option requires less startup time, but the speed of subsequent simulations is slower than with theCode generation
option. In this mode, you can debug the source code of the block.Code generation
— Simulate the model by using generated C code. The first time you run a simulation, Simulink generates C code for the block. The model reuses the C code for subsequent simulations unless the model changes. This option requires additional startup time, but the speed of the subsequent simulations is faster than with theInterpreted execution
option.
For more information, see Interpreted Execution vs. Code Generation (Simulink).
Block Characteristics
Data Types |
|
Multidimensional Signals |
|
Variable-Size Signals |
|
More About
Sample Timing
The time between output updates is equal to the product of the
Samples per frame and Sample time parameter
values. For example, if Sample time and Samples per
frame each equal 1
, the block outputs one sample every
second. If you increase Samples per frame to 10, then the block outputs
a 10-by-1 vector every 10 seconds. This timing ensures that the equivalent output rate is
not dependent on the Samples per frame parameter.
Tips
Set Source of initial
seed to Parameter
and make sure that all
blocks have different Initial seed values when
running simulations in parallel using the parsim
(Simulink) function, such as for Monte Carlo simulation runs.
Otherwise, the different workers may generate the same random numbers (leading to misleading results) for these model configurations:
If the model is in rapid accelerator mode.
If this block has Simulate using set to
Code generation
and Source of initial seed set toAuto
.
For more information, see Choosing a Simulation Mode (Simulink).
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.
Version History
Introduced before R2006aR2024a: Random number stream processing updates
Updates to the random number stream processing for the Random Integer Generator block remove some limitations. Before this update, certain operating modes resulted in the block generating the same numbers every time the simulation ran. This change removes the limitations noted for those operating modes.
R2024a: Seed value must be integer
The Random Integer Generator block requires a nonnegative integer value for the Seed parameter.
R2024a: R2015b Random Integer Generator block version no longer updated automatically
Starting in R2024a, models containing a Random Integer Generator block that has not been updated from the R2015b version, will need to be replaced with the current Random Integer Generator block version.
R2020a: Random Integer Generator block update supported in Upgrade Advisor
Starting in R2020a, Random Integer Generator block allows you to use the Upgrade Advisor. You can update to the block version announced in R2015b or keep the block version available before R2015b.
Use the Upgrade Advisor to update existing models that include the Random Integer Generator block.
Behavior of the random number generator is changed. The statistics are improved. For more information, see Source blocks output frames of contiguous time samples but do not use frame attribute.
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 (한국어)