필터 지우기
필터 지우기

programming on random number

조회 수: 2 (최근 30일)
RS
RS 2014년 7월 30일
댓글: RS 2014년 7월 30일
My problem is almost same as shown here ..I followed the same rule but the solution is become not applicable for large no...
I have generated 5000 random numbers from a normal distribution with std deviation=0.10 and mean=0.36
like
r=0.36+0.10*randn(1,5000)
now, I want to select 30 or 50 numbers from this random numbers, the summation of which will be in between 3.58 to 3.62..
I need 30 sets of such 30 numbers which sum will lie between this..3.58 to 3.62
my code which I did use is
% generate the random numbers
r = 0.36+0.1*randn(1,5000);
% pre-allocate memory for the 30 sets of 30 numbers
sets = zeros(30,30);
% generate each set
for i=1:30
% randomly choose 30 indices from list of random numbers
n = length(r);
idcs = randi(n,30,1);
% re-select the set of 30 indices if that set fails one of the three tests
while length(unique(idcs))~=30 || sum(r(idcs))<3.58 || sum(r(idcs))>3.62
idcs = randi(n,30,1);
end
% save the set of data corresponding to these indices
sets(:,i) = r(idcs);
% remove the 30 elements from r so that they are not picked again
% r(idcs) = [];
end
but this approaches are suitable for 10 numbers as shown in that question but logic is not good for 30 or 50 numbers..code takes indefinite time..
Is there any logic to solve this or any easy logic?

답변 (1개)

Michael Haderlein
Michael Haderlein 2014년 7월 30일
편집: Michael Haderlein 2014년 7월 30일
The code does indeed need infinite time, because the sum of 30 numbers around 0.36 will be around 10.8 (not 3.6). If you change your upper/lower limits, it's rather fast.
Edit: I just saw you want 10 numbers, not 30. So you have to change the number selection (before and in the loop), the length condition, and the sets preallocation.
  댓글 수: 1
RS
RS 2014년 7월 30일
yes you r right...its should be mean of 0.12.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by