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.

 채택된 답변

Star Strider
Star Strider 2022년 9월 24일

0 개 추천

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

Manu Chaudhary
Manu Chaudhary 2022년 9월 24일
Yes, in my code we are using randi but getting different results everytime.
Walter Roberson
Walter Roberson 2022년 9월 24일
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 2022년 9월 24일
이동: Star Strider 2022년 9월 24일
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 2022년 9월 24일
Thank you. Great help.
Star Strider
Star Strider 2022년 9월 24일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2022년 9월 24일

댓글:

2022년 9월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by