Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
generate specific random value except some numbers

Subject: generate specific random value except some numbers

From: Antonio Valentine

Date: 14 Jul, 2013 06:21:07

Message: 1 of 9

Hello there,

Kindly be asked for helping in the coding of how to generate a specific range of random numbers. Here is my question

I want to generate an integer number between 1-40.

randi(40,1)

However, I want to skip some numbers as appeared in this vector [10 13 22 27].

How should I do?

Thank you in advance.


This is what I have done in the past which is unable to scale up. So I would like to ask for suggestions.

actor_1 = 10;
actor_2 = 13;
actor_3 = 22;
 
num_event = 10;
num_node = 40;

for i = 1 : num_event
    
alarm_node(i) = randi(num_node,1);
    
while alarm_node(i) == actor_1 || alarm_node(i) == actor_2 %|| alarm_node(i) ~= actor_3
        
        alarm_node(i) = randi(num_node,1);
        
end

end

Subject: generate specific random value except some numbers

From: Nasser M. Abbasi

Date: 14 Jul, 2013 07:36:13

Message: 2 of 9

On 7/14/2013 1:21 AM, Antonio Valentine wrote:
> Hello there,
>
> Kindly be asked for helping in the coding of how to generate a specific range of random numbers. Here is my question
>
> I want to generate an integer number between 1-40.
>
> randi(40,1)
>
> However, I want to skip some numbers as appeared in this vector [10 13 22 27].
>
> How should I do?
>
> Thank you in advance.
>
>
> This is what I have done in the past which is unable to scale up. So I would like to ask for suggestions.
>
> actor_1 = 10;
> actor_2 = 13;
> actor_3 = 22;
>
> num_event = 10;
> num_node = 40;
>
> for i = 1 : num_event
>
> alarm_node(i) = randi(num_node,1);
>
> while alarm_node(i) == actor_1 || alarm_node(i) == actor_2 %|| alarm_node(i) ~= actor_3
>
> alarm_node(i) = randi(num_node,1);
>
> end
>
> end
>

-----------------------------
A = [1:9 11 12 13:21 23:26 28:40];
N = length(A);
num_event = 10;
arrayfun(@(i) A(randi(N,1)),1:num_event)
--------------------------------

  9 37 6 34 21 40 3 18 4 39

Subject: generate specific random value except some numbers

From: Antonio Valentine

Date: 14 Jul, 2013 07:43:07

Message: 3 of 9

"Nasser M. Abbasi" wrote in message <krtkdb$oj$1@speranza.aioe.org>...
> On 7/14/2013 1:21 AM, Antonio Valentine wrote:
> > Hello there,
> >
> > Kindly be asked for helping in the coding of how to generate a specific range of random numbers. Here is my question
> >
> > I want to generate an integer number between 1-40.
> >
> > randi(40,1)
> >
> > However, I want to skip some numbers as appeared in this vector [10 13 22 27].
> >
> > How should I do?
> >
> > Thank you in advance.
> >
> >
> > This is what I have done in the past which is unable to scale up. So I would like to ask for suggestions.
> >
> > actor_1 = 10;
> > actor_2 = 13;
> > actor_3 = 22;
> >
> > num_event = 10;
> > num_node = 40;
> >
> > for i = 1 : num_event
> >
> > alarm_node(i) = randi(num_node,1);
> >
> > while alarm_node(i) == actor_1 || alarm_node(i) == actor_2 %|| alarm_node(i) ~= actor_3
> >
> > alarm_node(i) = randi(num_node,1);
> >
> > end
> >
> > end
> >
>
> -----------------------------
> A = [1:9 11 12 13:21 23:26 28:40];
> N = length(A);
> num_event = 10;
> arrayfun(@(i) A(randi(N,1)),1:num_event)
> --------------------------------
>
> 9 37 6 34 21 40 3 18 4 39
>
>
>

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?

Subject: generate specific random value except some numbers

From: Bruno Luong

Date: 14 Jul, 2013 08:09:07

Message: 4 of 9

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

left=[A(1) B+1];
right=[B-1 A(end)];
n = right-left+1;
e = [0 cumsum(n)];
a = floor(e(end)*rand(1,1e5));
[~, d] = histc(a, e);
r = a+d+A(1)-1;

% Bruno

Subject: generate specific random value except some numbers

From: Antonio Valentine

Date: 14 Jul, 2013 08:30:07

Message: 5 of 9

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <krtmb3$1gq$1@newscl01ah.mathworks.com>...
> A = [1:40];
> B = [10 13 22 27];
>
> left=[A(1) B+1];
> right=[B-1 A(end)];
> n = right-left+1;
> e = [0 cumsum(n)];
> a = floor(e(end)*rand(1,1e5));
> [~, d] = histc(a, e);
> r = a+d+A(1)-1;
>
> % Bruno

It's quite complicated but it is working perfectly.

Thanksss Bruno. :)

Subject: generate specific random value except some numbers

From: Nasser M. Abbasi

Date: 15 Jul, 2013 03:32:57

Message: 6 of 9

On 7/14/2013 2:43 AM, Antonio Valentine wrote:

>>
>> -----------------------------
>> A = [1:9 11 12 13:21 23:26 28:40];
>> N = length(A);
>> num_event = 10;
>> arrayfun(@(i) A(randi(N,1)),1:num_event)
>> --------------------------------
>>
>> 9 37 6 34 21 40 3 18 4 39

>
> Your suggestion is quite straightforward.

But note: The above can end up picking the same element from
the set more than one time. Still by random, but you can
end up with the same element selected more than once.

If you want to randomly select say m elements from a set
without the same element selected again, you can try this:

------------------------------
   A = [1,2,3,5,6,7,11,12,20,21]; %the set
   m = 5; %number of elements to pick by random
   B = zeros(m,1); %place to store what was selected
   for i = 1:m
       k = randi(length(A),1);
       B(i) = A(k);
       A(k) = []; %remove what was selected so it won't be selected again
   end
   B
--------------------------


>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?
>

sorry, do not understand what is asked here.

--Nasser

Subject: generate specific random value except some numbers

From: Roger Stafford

Date: 15 Jul, 2013 09:26:10

Message: 7 of 9

"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

Subject: generate specific random value except some numbers

From: Antonio Valentine

Date: 20 Jul, 2013 07:09:12

Message: 8 of 9

"Roger Stafford" wrote in message <ks0f7i$4ct$1@newscl01ah.mathworks.com>...
> "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

Roger, it is excellent with a very short programming. Thank you.

Subject: generate specific random value except some numbers

From: Pablo Medina

Date: 19 Oct, 2014 22:09:11

Message: 9 of 9

"Roger Stafford" wrote in message <ks0f7i$4ct$1@newscl01ah.mathworks.com>...
> "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


Dear Roger,

With this code, the values generated have the same probability. For example with the folllowing values:
A=1:5;
B=2;
S = setdiff(A,B);
r = S(randi(numel(S),1,10));

Could you tell me the probability of any of r values?

thank you in advance

Kind regards,
Pablo

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us