Forums

Random numbers in MATLAB6

Started by Bart Hautala November 7, 2002
Saw the past post about generating random numbers from -100 to 100
but what if I needed to generate them from like -20 to -10? or 4.5
to 5?



Hi Bart,
A simple way that would work is to use rand, to first generate a value from 0 to
1. To get a value from -20 to -10, multiply by 10 then subtract 20. To get a
value from 4.5 to 5, multipily by 0.5 then add 4.5.
Hope that helps,
Quang
Bart Hautala <> wrote:Saw the past post about generating
random numbers from -100 to 100
but what if I needed to generate them from like -20 to -10? or 4.5
to 5?
_____________________________________
Note: If you do a simple "reply" with your email client, only the author of this
message will receive your answer. You need to do a "reply all" if you want your
answer to be distributed to the entire group.

_____________________________________
About this discussion group:

To Join:

To Post:

To Leave:

Archives: http://www.yahoogroups.com/group/matlab

More DSP-Related Groups: http://www.dsprelated.com/groups.php3

">http://docs.yahoo.com/info/terms/



Dear Hautala,

There is a simple general formula for generating random numbers using the
random function from Matlab.
Supose you need to generate 100 random numbers with uniform distribution,
beetween X1 and X2, where
X1 and X2 are both real numbers. The Matlab command stands like this:

random_vector = (x2-x1)*rand(1,100) + x1;

Type 'help rand' for more details on the rand function.

Best regards,

Hugo

----
Hugo de Paula
,
Center for Research on Speech, Acoustics, Language and Music, UFMG
ATR Human Information Science Lab
15-508 Takanohara-Ekinishi-Danchi, 1-2 Kabutodai, Kizu-cho, Souraku-gun,
Kyoto 619-0224
Tel: +81-774-73-9849 (home) +81-774-95-2624 (office)
----

----- Original Message -----
From: Bart Hautala
To:
Sent: Thursday, November 07, 2002 8:17 AM
Subject: [matlab] Random numbers in MATLAB6 Saw the past post about generating random numbers from -100 to 100
but what if I needed to generate them from like -20 to -10? or 4.5
to 5?

--
+++ GMX - Mail, Messaging & more http://www.gmx.net +++
NEU: Mit GMX ins Internet. Rund um die Uhr f 1 ct/ Min. surfen!


Hai,
You can use 'rand' function which generates random numbers from
0 to 1 and generate in which range you want.
eg: To generate random numbers from -20 to -10
x = -20+10*rand will do
To generate random numbers from 4.5 to 5
x = 4.5+rand/2 may solve the purpose.
...........Ananth.
Bart Hautala <> wrote:Saw the past post about generating
random numbers from -100 to 100
but what if I needed to generate them from like -20 to -10? or 4.5
to 5?
_____________________________________
Note: If you do a simple "reply" with your email client, only the author of this
message will receive your answer. You need to do a "reply all" if you want your
answer to be distributed to the entire group.

_____________________________________
About this discussion group:

To Join:

To Post:

To Leave:

Archives: http://www.yahoogroups.com/group/matlab

More DSP-Related Groups: http://www.dsprelated.com/groups.php3

YOU MAY NOT LOVE YOUR ENEMY BUT PLEASE DON'T HATE HIM
K.V.K.ANANTHANAG, R.NO:207,HOSTEL: DIHING, IIT GUWAHATI,GUWAHATI-781039.
Phone:(0361)690763




> Saw the past post about generating random numbers from -100 to 100
> but what if I needed to generate them from like -20 to -10? or 4.5
> to 5?

If you can generate random numbers between -100 and 100, then you can
divide these numbers by 20 and subtract 10 from them to get something in
the range-20 to -10, or by 400 and add 4.75 to get the range 4.5 to 5.

In general if you want to generate numbers i the range x to y, divide your
random num,bers which are between -100 and 100 by 200, multiply by y-x,
and then add (y+x)/2

Tim