generate random number between positive and negative and sum of this number is zero

7 ビュー (過去 30 日間)
I want generate random number between positive and negative, and then the sum of this number equal to zero. what have I should do?

採用された回答

Bruno Luong
Bruno Luong 2022 年 4 月 12 日
  1 件のコメント
kukuh widarsono
kukuh widarsono 2022 年 4 月 12 日
great sir...thanks you very much. I think, it suits with my problem

サインインしてコメントする。

その他の回答 (1 件)

Voss
Voss 2022 年 4 月 12 日
x = randn(); % random number from standard Normal distribution
y = -x; % another number such that the sum of x and y is 0
x
x = -0.4716
y
y = 0.4716
x+y
ans = 0
  2 件のコメント
kukuh widarsono
kukuh widarsono 2022 年 4 月 12 日
thanks. but I want make array/matrix in my program...not just one time or one number.
Voss
Voss 2022 年 4 月 12 日
x = randn(5); % 5x5 random matrix from standard Normal distribution
y = -x; % another matrix such that the sum of x and y is 0
x
x = 5×5
-1.3777 -0.9362 -1.0312 -2.0870 0.9580 0.1070 1.1208 -0.8803 -0.0622 0.7201 1.1074 0.3074 0.7390 -0.3496 0.2003 -1.2134 0.5940 -0.3744 0.3251 0.3406 0.5072 0.3389 -1.6013 1.9403 0.2310
y
y = 5×5
1.3777 0.9362 1.0312 2.0870 -0.9580 -0.1070 -1.1208 0.8803 0.0622 -0.7201 -1.1074 -0.3074 -0.7390 0.3496 -0.2003 1.2134 -0.5940 0.3744 -0.3251 -0.3406 -0.5072 -0.3389 1.6013 -1.9403 -0.2310
x+y
ans = 5×5
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by