Got Questions? Get Answers.
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 random numbers

Subject: generate random numbers

From: Amy

Date: 3 Apr, 2012 02:46:13

Message: 1 of 5

How can I generate random values within a range in Matlab? Let's say I need to generate random numbers between [65, 85]
thanks for helping

Subject: generate random numbers

From: Steven_Lord

Date: 3 Apr, 2012 13:59:14

Message: 2 of 5



"Amy " <amyd81@gmail.com> wrote in message
news:jldo9k$ev9$1@newscl01ah.mathworks.com...
> How can I generate random values within a range in Matlab? Let's say I
> need to generate random numbers between [65, 85]
> thanks for helping

Depending on the distribution take a look at RAND, RANDN, RANDI, or RANDPERM
in MATLAB or the random number generator functions in Statistics Toolbox.

http://www.mathworks.com/help/toolbox/stats/bq_w_hm.html#bq_w_ie-15

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: generate random numbers

From: ahmed elziery

Date: 3 Apr, 2012 17:23:13

Message: 3 of 5

"Amy" wrote in message <jldo9k$ev9$1@newscl01ah.mathworks.com>...
> How can I generate random values within a range in Matlab? Let's say I need to generate random numbers between [65, 85]
> thanks for helping
okey here is the solution
you can loop between the these two numbers
and write this condition if(65<rand<85)
arrayb(j)=rand;
end

Subject: generate random numbers

From: Jomar Bueyes

Date: 3 Apr, 2012 17:35:38

Message: 4 of 5

On Tuesday, April 3, 2012 1:23:13 PM UTC-4, ahmed elziery wrote:
> "Amy" wrote in message <jldo9k$ev9$1@newscl01ah.mathworks.com>...
> > How can I generate random values within a range in Matlab? Let's say I need to generate random numbers between [65, 85]
> > thanks for helping
> okey here is the solution
> you can loop between the these two numbers
> and write this condition if(65<rand<85)
> arrayb(j)=rand;
> end

The above won't work because 1) rand returns pseudo random numbers in the open interval (0,1) and thus the condition 65<rand<85 is never satisfied. 2) Even if rand would return numbers in a range that includes 65 to 85, the function is called twice, the first time to use in the if statement and the second to assign a random number to arrayb(j). That is, the test is done on one pseudo random number but a different one is assigned to arrayb(j) and this second number is not necessarily in the (85, 85) interval. As Steve Lord suggested, see the help for the rand function. It explains how to get pseudo random numbers in arbitrary intervals.

HTH

Jomar

Subject: generate random numbers

From: Steven_Lord

Date: 3 Apr, 2012 19:44:44

Message: 5 of 5



"Jomar Bueyes" <jomarbueyes@hotmail.com> wrote in message
news:32607297.2442.1333474538977.JavaMail.geo-discussion-forums@vbuc18...
> On Tuesday, April 3, 2012 1:23:13 PM UTC-4, ahmed elziery wrote:
>> "Amy" wrote in message <jldo9k$ev9$1@newscl01ah.mathworks.com>...
>> > How can I generate random values within a range in Matlab? Let's say I
>> > need to generate random numbers between [65, 85]
>> > thanks for helping
>> okey here is the solution
>> you can loop between the these two numbers
>> and write this condition if(65<rand<85)
>> arrayb(j)=rand;
>> end
>
> The above won't work because 1) rand returns pseudo random numbers in the
> open interval (0,1) and thus the condition 65<rand<85 is never satisfied.

As written, it is ALWAYS satisfied. 65 < rand < 85 is equivalent to (65 <
rand) < 85. Since RAND returns values in the interval (0, 1) the expression
inside the parentheses is always false or 0 and since 0 < 85, the expression
is true.

What the poster to whom you're replying meant to write was something along
the lines of "x = rand; if (65 < x) && (x < 85), ..." which would never be
satisfied, as you realized.

>2) Even if rand would return numbers in a range that includes 65 to 85, the
>function is called twice, the first time to use in the if statement and the
>second to assign a random number to arrayb(j). That is, the test is done on
>one pseudo random number but a different one is assigned to arrayb(j) and
>this second number is not necessarily in the (85, 85) interval.

With the obvious typo fixed, you are correct.

> As Steve Lord suggested, see the help for the rand function. It explains
> how to get pseudo random numbers in arbitrary intervals.

RAND may or may not be the answer the OP was looking for; there's not enough
information to determine their desired answer. We'd need to know the
distribution they wanted.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

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