Hi everyone, I have this below code of matlab, which generates random values.
data_size = 8; % not necessarily a power-of-2
data_range = [0 255];
data = randi(data_range, data_size, 1) + 1i*randi(data_range, data_size, 1); % Complex Data
I am new at matlab. Is there a way to fix these random values. I need it because i am stuck with some debugging.

 Akzeptierte Antwort

Star Strider
Star Strider am 24 Sep. 2022

0 Stimmen

The rng function controls the seed.
Example —
rng(1)
r1 = randi(9,1,10)
r1 = 1×10
4 7 1 3 2 1 2 4 4 5
rng(1)
r2 = randi(9,1,10)
r2 = 1×10
4 7 1 3 2 1 2 4 4 5
There are a number of helpful links in the See Also section of the documentation that explain it.
.

7 Kommentare

Manu Chaudhary
Manu Chaudhary am 24 Sep. 2022
Yes, in my code we are using randi but getting different results everytime.
Walter Roberson
Walter Roberson am 24 Sep. 2022
rng() controls the sequence of values.
You will need to set the rng seed every time before calling any of the random number generators.
My code example illustrates that.
However once reset, the random numbers should produce the same result, regardless of the number of times they are called —
rng(1)
r1a = randi(9, 10, 1) % First Call
r1a = 10×1
4 7 1 3 2 1 2 4 4 5
r1b = randi(9, 10, 1) % Second Call
r1b = 10×1
4 7 2 8 1 7 4 6 2 2
rng(1)
r2 = randi(9, 20, 1) % First Call, Length Doubled
r2 = 20×1
4 7 1 3 2 1 2 4 4 5
r2bfr = buffer(r2, 10) % Use 'buffer' To See Entire Array
r2bfr = 10×2
4 4 7 7 1 2 3 8 2 1 1 7 2 4 4 6 4 2 5 2
.
Manu Chaudhary
Manu Chaudhary am 24 Sep. 2022
Verschoben: Star Strider am 24 Sep. 2022
data_size = 8; % not necessarily a power-of-2
data_range = [0 255];
data = rng(randi(data_range, data_size, 1) + 1i*randi(data_range, data_size, 1)); % Complex Data
Tried rng but this code is now throwing error? Any way to fix it.
The rng call should be entirely separate, and be placed before the first call to the random number generators —
data_size = 8; % not necessarily a power-of-2
data_range = [0 255];
rng(1)
data = randi(data_range, data_size, 1) + 1i*randi(data_range, data_size, 1) % Complex Data
data =
1.0e+02 * 1.0600 + 1.0100i 1.8400 + 1.3700i 0.0000 + 1.0700i 0.7700 + 1.7500i 0.3700 + 0.5200i 0.2300 + 2.2400i 0.4700 + 0.0700i 0.8800 + 1.7100i
rng(1)
data = randi(data_range, data_size, 1) + 1i*randi(data_range, data_size, 1) % Complex Data
data =
1.0e+02 * 1.0600 + 1.0100i 1.8400 + 1.3700i 0.0000 + 1.0700i 0.7700 + 1.7500i 0.3700 + 0.5200i 0.2300 + 2.2400i 0.4700 + 0.0700i 0.8800 + 1.7100i
That now runs without error and produces the desired result.
I duplicated the rng call and the ‘data’ assignment to demonstrate that. (The duplicate can be deleted, since I am simply demonstrating the effect here.)
.
Manu Chaudhary
Manu Chaudhary am 24 Sep. 2022
Thank you. Great help.
Star Strider
Star Strider am 24 Sep. 2022
As always, my pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by