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