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:
How to generate ONE random number between TWO intervals??

Subject: How to generate ONE random number between TWO intervals??

From: weg22@drexel.edu

Date: 30 Sep, 2008 14:19:28

Message: 1 of 6

Hi all,

I need to generate a single random number between two intervals. For
example, how would I use the "rand()" function to generate a random
number between the intervals of: [-2, -1] AND [1,2]. If I needed a
random number between [1,2], I know I can just simply use:

r = a + (b-a).*rand(100,1);

where a=1 and b=2. Any ideas on how to apply this equation to get a
random number between the 2 intervals listed above?

Thanks in advance,
-weg22

Subject: How to generate ONE random number between TWO intervals??

From: James Tursa

Date: 30 Sep, 2008 14:36:02

Message: 2 of 6

weg22@drexel.edu wrote in message <d9ac14bd-ee11-4c3b-b8cd-d4bc7f113d18@t41g2000hsc.googlegroups.com>...
> Hi all,
>
> I need to generate a single random number between two intervals. For
> example, how would I use the "rand()" function to generate a random
> number between the intervals of: [-2, -1] AND [1,2]. If I needed a
> random number between [1,2], I know I can just simply use:
>
> r = a + (b-a).*rand(100,1);
>
> where a=1 and b=2. Any ideas on how to apply this equation to get a
> random number between the 2 intervals listed above?
>
> Thanks in advance,
> -weg22

Generate r for the interval [0,2], then shift part of it. e.g.,

r = 2*rand(100,1);
x = r<1;
r(x) = r(x) - 2;

James Tursa

Subject: How to generate ONE random number between TWO intervals??

From: John D'Errico

Date: 30 Sep, 2008 14:39:02

Message: 3 of 6

weg22@drexel.edu wrote in message <d9ac14bd-ee11-4c3b-b8cd-d4bc7f113d18@t41g2000hsc.googlegroups.com>...
> Hi all,
>
> I need to generate a single random number between two intervals. For
> example, how would I use the "rand()" function to generate a random
> number between the intervals of: [-2, -1] AND [1,2]. If I needed a
> random number between [1,2], I know I can just simply use:
>
> r = a + (b-a).*rand(100,1);
>
> where a=1 and b=2. Any ideas on how to apply this equation to get a
> random number between the 2 intervals listed above?
>
> Thanks in advance,
> -weg22

What does "between two intervals" mean?
I don't think it has a mathematical definition.
Be precise.

Do you mean something that has a uniform
distribution over the disjoint domain as
defined by those two intervals?

Since they both have the same length, and
they are symmetric around zero, you might
just choose a random number in the interval
[1,2], and then choose a random sign to apply.
(I won't give you any more than this for such
a likely homework problem.)

If the intervals were not the same length,
say [-3,-1] and [1,2], it is still easy enough.
Can you choose a random number in the
interval [0,3]? Now, what could you do if that
number actually fell in the subinterval [0,2]?
What might you do if it fell in the subinterval
[2,3]? If is your friend here, since you intend
to generate only one single number.

John

Subject: How to generate ONE random number between TWO intervals??

From: Jos

Date: 30 Sep, 2008 14:51:02

Message: 4 of 6

weg22@drexel.edu wrote in message <d9ac14bd-ee11-4c3b-b8cd-d4bc7f113d18@t41g2000hsc.googlegroups.com>...
> Hi all,
>
> I need to generate a single random number between two intervals. For
> example, how would I use the "rand()" function to generate a random
> number between the intervals of: [-2, -1] AND [1,2]. If I needed a
> random number between [1,2], I know I can just simply use:
>
> r = a + (b-a).*rand(100,1);
>
> where a=1 and b=2. Any ideas on how to apply this equation to get a
> random number between the 2 intervals listed above?
>
> Thanks in advance,
> -weg22

Since these intervals are mirrored around zero, you could use:

a = 1 ; b = 2 ;
r = a + (b-a).*rand(100,1);
s = round(rand(size(r)))==1 ; % sign
r = (2*s-1) .* r ;

Jos

Subject: How to generate ONE random number between TWO intervals??

From: Vincent

Date: 10 Apr, 2013 20:58:06

Message: 5 of 6

weg22,

I had a similar question. Here's the solution I used assuming that the two intervals are mirrored about zero:

r = (a + (b - a)*rand) * (-1)^floor(rand*2);

- first part gives a random number between a and b.
- second part attached a random sign by multiplying by -1^0 = 1 or -1^1 = 1

Best,
Vince



weg22@drexel.edu wrote in message <d9ac14bd-ee11-4c3b-b8cd-d4bc7f113d18@t41g2000hsc.googlegroups.com>...
> Hi all,
>
> I need to generate a single random number between two intervals. For
> example, how would I use the "rand()" function to generate a random
> number between the intervals of: [-2, -1] AND [1,2]. If I needed a
> random number between [1,2], I know I can just simply use:
>
> r = a + (b-a).*rand(100,1);
>
> where a=1 and b=2. Any ideas on how to apply this equation to get a
> random number between the 2 intervals listed above?
>
> Thanks in advance,
> -weg22

Subject: How to generate ONE random number between TWO intervals??

From: Roger Stafford

Date: 10 Apr, 2013 23:13:10

Message: 6 of 6

weg22@drexel.edu wrote in message <d9ac14bd-ee11-4c3b-b8cd-d4bc7f113d18@t41g2000hsc.googlegroups.com>...
> I need to generate a single random number between two intervals. ......
- - - - - - - -
  In general for two separate intervals [a,b] and [c,d] where a < b < c < d, to get a uniform distribution within them, modify James' method:

 r = (b-a+d-c)*rand(n,1)+a;
 r = r+(c-b)*(r>=b);

Roger Stafford

Tags for 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