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.
採用された回答
Star Strider
2022 年 9 月 24 日
7 件のコメント
Manu Chaudhary
2022 年 9 月 24 日
Yes, in my code we are using randi but getting different results everytime.
Walter Roberson
2022 年 9 月 24 日
rng() controls the sequence of values.
Star Strider
2022 年 9 月 24 日
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.
Star Strider
2022 年 9 月 24 日
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
2022 年 9 月 24 日
Thank you. Great help.
Star Strider
2022 年 9 月 24 日
As always, my pleasure!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Random Number Generation についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
