Square of 20 random number sum up to 1

1 回表示 (過去 30 日間)
shawin
shawin 2018 年 9 月 6 日
編集済み: Dimitris Kalogiros 2018 年 9 月 6 日
The code below generate 20random number which their sum up to 1. r = rand(1, 20); % Start with 20 random numbers. r = r / sum(r) % Normalize so the sum is 1. theSum = sum(r) % Check to make sure. Should be 1
How we can generates 20 random number in which their squares are sum up to 1.

回答 (2 件)

Dimitris Kalogiros
Dimitris Kalogiros 2018 年 9 月 6 日
編集済み: Dimitris Kalogiros 2018 年 9 月 6 日
clear ; clc;
N=20;
r=rand(1,N);
normFactor=sqrt(sum(r.^2));
r=r/normFactor;
figure;
plot(r,'bo');
zoom on; grid on;
xlabel('index'); ylabel('random number');
S=sum(r.^2);
fprintf('sum of square random numbers : %f \n', S);
If you run this script :

Torsten
Torsten 2018 年 9 月 6 日
r = r / sqrt(sum(r.^2))
Best wishes
Torsten.

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by