COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Random vector generation

• Email
• Follow

```How to generate independent identically distributed random vectors of finite length using MATLAB?
```
 0

See related articles to this posting

```"Govindarajan Jagannathan" <govind.ipr@gmail.com> wrote in message <i7bvl7\$4id\$1@fred.mathworks.com>...
> How to generate independent identically distributed random vectors of finite length using MATLAB?

The rand function produces uniformly distributed random numbers.
randn produces normally-distributed numbers.

Does that help?

Ross
```
 0

```"Govindarajan Jagannathan" <govind.ipr@gmail.com> wrote in message <i7bvl7\$4id\$1@fred.mathworks.com>...
> How to generate independent identically distributed random vectors of finite length using MATLAB?

If you want a uniform distribution in the ball { x in R^d : |x| <= rmax }, then this should do:

d = 3; % dimension
rmax = 3; %
n = 1000; % number of vectors

v = randn(d,n);
r = (rand(1,n)*rmax^d).^(1/d);
c = r./sqrt(sum(v.^2,1));
v = bsxfun(@times, v, c);

Bruno
```
 0
Reply b.luong5955 (6403) 9/22/2010 5:27:04 AM

```This might convince the above code works - at least in 2D:

d = 2; % dimension
rmax = 5;
n = 10000; % number of vectors

v = randn(d,n);
r = rmax * rand(1,n).^(1/d);
c = r./sqrt(sum(v.^2,1));
v = bsxfun(@times, v, c);

plot(v(1,:),v(2,:),'.')
axis equal

% Bruno
```
 0

```Thank you very much for both the messages. I will try them out.

govind

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <i7c4k4\$ods\$1@fred.mathworks.com>...
> This might convince the above code works - at least in 2D:
>
> d = 2; % dimension
> rmax = 5;
> n = 10000; % number of vectors
>
> v = randn(d,n);
> r = rmax * rand(1,n).^(1/d);
> c = r./sqrt(sum(v.^2,1));
> v = bsxfun(@times, v, c);
>
> plot(v(1,:),v(2,:),'.')
> axis equal
>
> % Bruno
```
 0

```Thank you for the information, Ross. It sure does help.

--govind

"Ross W" <rosswoodskiwi@hotmail.com> wrote in message <i7c3cr\$7dd\$1@fred.mathworks.com>...
> "Govindarajan Jagannathan" <govind.ipr@gmail.com> wrote in message <i7bvl7\$4id\$1@fred.mathworks.com>...
> > How to generate independent identically distributed random vectors of finite length using MATLAB?
>
> The rand function produces uniformly distributed random numbers.
> randn produces normally-distributed numbers.
>
> Does that help?
>
> Ross
```
 0