programming on random number

3 visualizzazioni (ultimi 30 giorni)
RS
RS il 30 Lug 2014
Commentato: RS il 30 Lug 2014
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?

Risposte (1)

Michael Haderlein
Michael Haderlein il 30 Lug 2014
Modificato: Michael Haderlein il 30 Lug 2014
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 Commento
RS
RS il 30 Lug 2014
yes you r right...its should be mean of 0.12.

Accedi per commentare.

Categorie

Scopri di più su Random Number Generation in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by