random 2dimensional matrix


Taiseer
02-09-2011, 10:51 AM
Hi,

how to generate a 2dimensional matrix (n,m) that all it elements are random numbers between -1 and 1.

Thanks

davekw7x
02-10-2011, 08:21 AM
...generate a 2dimensional matrix (n,m)Declare a 2-Dimensional array of reals.

that all it elements
Make nested loops like this:

do i=1,m
do j=1,n
! Get a value to store into x(i, j)
val = "something"

! Store the value
x(i,j) = val
enddo
enddo

...random numbers between -1 and 1...

Do you mean to get deviates from some PRNG (Pseudo Random Number Generator) that represent samples from a population that is uniformly distributed on the interval -1 to 1?

Well, typical PRNGs, like Numerical Recipes ran2 yield samples that are (approximately) uniformly distributed on the interval 0 to 1, so you simply have to map the interval [0,1] to [-1,1], and the distribution of the results will be uniformly distributed over the new interval, right?

In general, you can map the interval [0,1] to the interval [a,b] by the following:

Suppose u is a random variable uniformly distributed on the interval [0, 1] and you want a random variable v to be uniformly distributed on [a, b]

Then
v = a + (b-a) * u

So the loop stuff can look like


do i=1,m
do j=1,n
call ran2(val)
x(i,j) = 2.0*val - 1.0
enddo
enddo



Now if this is not what you had in mind, how about giving us a complete and precise program specification?


Regards,

Dave