any logic to do this programming on random number

3 ビュー (過去 30 日間)
joy
joy 2014 年 6 月 11 日
編集済み: John D'Errico 2014 年 6 月 11 日
Hi,
I have generated 1000 random numbers from a normal distribution with std deviation=0.10 and mean=0.36
like
r=0.36+0.10*randn(1,1000)
now, I want to select 10 numbers from this random numbers, the summation of which will be in between 3.55 to 3.65..
I need 30 sets of such 10 numbers which sum will lie between this..3.55 to 3.65
any logic to do so
thanks
  2 件のコメント
Geoff Hayes
Geoff Hayes 2014 年 6 月 11 日
編集済み: Geoff Hayes 2014 年 6 月 11 日
Note that the above equation is not quite correct (or your standard deviation and mean are reversed). If std is 0.1 and the mean is 0.36, then
r = 0.36+0.1*randn(1,1000);
Try std(r) and mean(r) to verify this.
joy
joy 2014 年 6 月 11 日
yes...u r right..thank u

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

採用された回答

Geoff Hayes
Geoff Hayes 2014 年 6 月 11 日
From the set of 1000 random numbers, you could do something like the following
% generate the random numbers
r = 0.36+0.1*randn(1,1000);
% pre-allocate memory for the 30 sets of 10 numbers
sets = zeros(10,30);
% generate each set
for i=1:30
% randomly choose 10 indices from list of random numbers
n = length(r);
idcs = randi(n,10,1);
% re-select the set of 10 indices if that set fails one of the three tests
while length(unique(idcs))~=10 || sum(r(idcs))<3.55 || sum(r(idcs))>3.66
idcs = randi(n,10,1);
end
% save the set of data corresponding to these indices
sets(:,i) = r(idcs);
% remove the 10 elements from r so that they are not picked again
r(idcs) = [];
end
Of course, the problem with the above is the while loop - if more sets are chosen, then it may become more difficult to find 10 such elements that satisfy the sum requirements and so the code may (eventually) become stuck in the loop.

その他の回答 (1 件)

Sean de Wolski
Sean de Wolski 2014 年 6 月 11 日
編集済み: Sean de Wolski 2014 年 6 月 11 日
I would take a different approach. First generate the 30 numbers who you must sum to:
xsum = rand(1,30)+3.55;
Now generate the 10 random numbers that sum to xsum using Roger's randfixedsum
Example:
%%Desired sums
xsum = rand(1,30)+3.55;
%%build random numbers
rn = zeros(10,30);
for ii = 1:30
rn(:,ii) = randfixedsum(10,1,xsum(ii),0,xsum(ii));
end
%%verify
assert(norm(sum(rn)-xsum)<10^-14)
  3 件のコメント
Sean de Wolski
Sean de Wolski 2014 年 6 月 11 日
Ahh! Good catch.
Sean de Wolski
Sean de Wolski 2014 年 6 月 11 日
I'll have to think about it a little more.

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

カテゴリ

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