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:
picking random numbers between a certain range

Subject: picking random numbers between a certain range

From: Kenzo Mendoza

Date: 18 Feb, 2010 15:50:23

Message: 1 of 6

Hi
I would like to initialize a vector with random numbers between -2 and 2

would it be something like x=rand(1,10) *2?

Any response will be greatly appreciated

Subject: picking random numbers between a certain range

From: Jan Simon

Date: 18 Feb, 2010 16:10:24

Message: 2 of 6

Dear Kenzo!

> I would like to initialize a vector with random numbers between -2 and 2
>
> would it be something like x=rand(1,10) *2?

Why do you ask us and not Matlab? Do you have a Matlab instance for trying it?
If not, you'd observe that your command creates number between 0 and 2.

Good luck, Jan

Subject: picking random numbers between a certain range

From: Oleg Komarov

Date: 18 Feb, 2010 16:10:24

Message: 3 of 6

"Kenzo Mendoza" <kenzo_mendoza@knights.ucf.edu> wrote in message <hljnjv$7kr$1@fred.mathworks.com>...
> Hi
> I would like to initialize a vector with random numbers between -2 and 2
>
> would it be something like x=rand(1,10) *2?
>
> Any response will be greatly appreciated

help rand

Oleg

Subject: picking random numbers between a certain range

From: Sadik

Date: 18 Feb, 2010 16:27:03

Message: 4 of 6

"Kenzo Mendoza" <kenzo_mendoza@knights.ucf.edu> wrote in message <hljnjv$7kr$1@fred.mathworks.com>...
> Hi
> I would like to initialize a vector with random numbers between -2 and 2
>
> would it be something like x=rand(1,10) *2?
>
> Any response will be greatly appreciated

Maybe it is more of a statistics question.

If U is uniformly distributed over [0,1), then 4*U-2 will be uniformly distributed over [-2,2).

Then, x = 4*rand(1,10)-2; would do it for you.

Best.

Subject: picking random numbers between a certain range

From: Wayne King

Date: 18 Feb, 2010 16:32:09

Message: 5 of 6

"Kenzo Mendoza" <kenzo_mendoza@knights.ucf.edu> wrote in message <hljnjv$7kr$1@fred.mathworks.com>...
> Hi
> I would like to initialize a vector with random numbers between -2 and 2
>
> would it be something like x=rand(1,10) *2?
>
> Any response will be greatly appreciated

In addition to the help other posters have given you, if you have the Statistics Toolbox, you can use unifrnd()

R = unifrnd(-2,2,100,1);
hist(R)

Wayne

Subject: picking random numbers between a certain range

From: Oleg Komarov

Date: 18 Feb, 2010 16:38:05

Message: 6 of 6

> Maybe it is more of a statistics question.
>
> If U is uniformly distributed over [0,1), then 4*U-2 will be uniformly distributed over [-2,2).
>
> Then, x = 4*rand(1,10)-2; would do it for you.
>
> Best.

Statistic or not, the example in the help is clear (and people should learn to read the help of a fcn before asking):

Examples:
        Generate values from the uniform distribution on the interval [a, b].
          r = a + (b-a).*rand(100,1);

Oleg

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