Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Other Programming Languages (http://www.programmingforums.org/forum38.html)
-   -   random numbers fortran 90 (http://www.programmingforums.org/showthread.php?t=15136)

lizi_n Feb 7th, 2008 5:09 PM

random numbers fortran 90
 
I need to randomly generate an equal number of +,- 1's. I have used [call random_number] and made every number >0.5=1 and any number <0.5=-1. It's in a loop from 1..n but I can't work out how to make sure that there are exactly the same amount of each. Please help!
Lizi

Sane Feb 7th, 2008 6:09 PM

Re: random numbers fortran 90
 
You want an equal amount, but you want it random? A little strange, but okay. I'd suggest the following.

If you want there to be 10 numbers, then, make an array of 5 ones and 5 negative ones.

{+1, +1, +1, +1, +1, -1, -1, -1, -1, -1}

Loop through each index. Each time, generate a random number between (0 - 9). Use that number to determine which position the current element should be swapped with. Swapping 10 elements 10 times, will effectively produce a random array of perfectly uniform distribution.

:

0th index
Random Number: 9
Swap 9th element with 0th element
{-1, +1, +1, +1, +1, -1, -1, -1, -1, +1}

1st index
Random Number: 2
Swap 2nd element with 1st element
{-1, +1, +1, +1, +1, -1, -1, -1, -1, +1}

... etc ...

9th index
Random Number: 5
Swap 9th element with 5th element
{+1, +1, -1, -1, +1, +1, -1, -1, +1, -1}


In Pseudocode:

:

n = 10

Make array X [Size n]
X[0 .. n/2-1] = {1}
X[n/2 .. n-1] = {-1}

for i : 0 ... n-1
    j = random number (0, n-1)
    swap(X[i], X[j])

output X


lizi_n Feb 11th, 2008 3:09 PM

Re: random numbers fortran 90
 
Thank you that's awesome. Could you just tell me what I write in order to swap the elements. At the moment I think I'm just replacing them by typing

x(i)=x(j)
x(j)=x(i)

so I end up with an uneven amount of +1 and -1.
Thank you!

lizi_n Feb 11th, 2008 3:18 PM

Re: random numbers fortran 90
 
This is what I've got so far if it helps.

:

integer:: n
parameter(n=10)

integer:: i,t,h
real:: s
integer, dimension(n) :: x

do i=1,n/2
x(i)=1
end do

do i=n/2+1,n
x(i)=-1
end do

print '(10i2)',x

do i=1,n

call random_number(s)
s=n*s

do h=0,n-1
if(s.gt.h.and.s.lt.h+1) t=h+1
end do


x(i)=x(t)
x(t)=x(i)

print*,x(i)


end do


Sane Feb 11th, 2008 3:30 PM

Re: random numbers fortran 90
 
:

x(i)=x(j)
x(j)=x(i)


That is close, but examine what happens when you swap two elements. The value of the first element is overwritten, by the time you assign it a new value.

:

E.G. Let x(i) be 5, and x(j) be 6.

x(i) becomes x(j) = 6
x(j) becomes x(i) = 6 as well


The solution to this is to use a temporary variable, let's say temp, to remember the first value, before the swapping occurs.

:

temp = x(i)
x(i) = x(j)
x(j) = temp



All times are GMT -5. The time now is 3:35 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC