DSPRelated.com
Forums

Random number selection

Started by durian_world April 29, 2003
Dear all,

I would like to select a number of entries from a fix set. Say i want
to select 4 numbers from the set [1 45 26 987 25 97 65 89 46]. One
possible result can be [56 987 65 46].
How can I achieve that?

Thanks in advance!

J.Y.



Hy JY,

You can add the following lines to your program:

M=[1 45 26 987 25 97 65 89 46];
for i=1:4
result(1,i)=M(1,ceil(9*rand)); %you have 9 numbers in your list
end

regards
Rene Bartar

>Dear all,
>
>I would like to select a number of entries from a fix set. Say i want
>to select 4 numbers from the set [1 45 26 987 25 97 65 89 46]. One
>possible result can be [56 987 65 46].
>How can I achieve that?
>
>Thanks in advance!
>
>J.Y.


Hi, folks!! Sorry for the slip, so lets try to fix it.

pointer=[10 10 10 10]; %thats the variable that will point the position in
you matrix.
flag=0; %flag==1 means "Hey! You've already picked that number!"
M=[1 45 26 987 25 97 65 89 46];
for i=1:4
while flag==1 do
flag=0;
pointer[i]l(9*rand)
for j=1:i
if pointer[i]==pointer[j]
flag=1
end
end
result(1,i)=M(pointer(i));
end

I think that will do, but i didnt test it for lack of time.

regards
Rene Bartar >Hi, I believe the attached code could result in numbers selected twice.
>for that reason, the procedure need little fine tuning to make sure that
>the number has not been selected. >
>RenBartar <> wrote:
>Hy JY,
>
>You can add the following lines to your program:
>
>M=[1 45 26 987 25 97 65 89 46];
>for i=1:4
>result(1,i)=M(1,ceil(9*rand)); %you have 9 numbers in your list
>end
>
>regards
>Rene Bartar
>
> >Dear all,
> >
> >I would like to select a number of entries from a fix set. Say i want
> >to select 4 numbers from the set [1 45 26 987 25 97 65 89 46]. One
> >possible result can be [56 987 65 46].
> >How can I achieve that?
> >
> >Thanks in advance!
> >
> >J.Y.