Fix the random values
显示 更早的评论
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.
采纳的回答
7 个评论
Yes, in my code we are using randi but getting different results everytime.
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
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.)
.
Thank you. Great help.
As always, my pleasure!
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
