Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: generate specific random value except some numbers
Replies: 6   Last Post: Jul 15, 2013 5:26 AM

 Messages: [ Previous | Next ]
 Roger Stafford Posts: 5,929 Registered: 12/7/04
Re: generate specific random value except some numbers
Posted: Jul 15, 2013 5:26 AM

"Antonio Valentine" <chanloha@gmail.com> wrote in message <krtkqb\$rsc\$1@newscl01ah.mathworks.com>...
> Your suggestion is quite straightforward. May I ask for an option of
>
> A = [1:40];
> B = [10 13 22 27];
>
> Then I can separate into two vectors where vector B the numbers I want to skip it.
>
> Is that possible?

- - - - - - - - - - - - -
A = 1:40;
B = [10 13 22 27];

S = setdiff(A,B);
r = S(randi(numel(S),n,1));

where n is the desired number of random values.

Note: This should work for any sets A and B, integer-valued or otherwise, provided their 'setdiff' is not empty.

Roger Stafford

Date Subject Author
7/14/13 Antonio Valentine
7/14/13 Nasser Abbasi
7/14/13 Antonio Valentine
7/14/13 Bruno Luong
7/14/13 Antonio Valentine
7/14/13 Nasser Abbasi
7/15/13 Roger Stafford