Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 7th, 2008, 5:09 PM   #1
lizi_n
Newbie
 
Join Date: Feb 2008
Posts: 3
Rep Power: 0 lizi_n is on a distinguished road
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
lizi_n is offline   Reply With Quote
Old Feb 7th, 2008, 6:09 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
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
Sane is offline   Reply With Quote
Old Feb 11th, 2008, 3:09 PM   #3
lizi_n
Newbie
 
Join Date: Feb 2008
Posts: 3
Rep Power: 0 lizi_n is on a distinguished road
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 is offline   Reply With Quote
Old Feb 11th, 2008, 3:18 PM   #4
lizi_n
Newbie
 
Join Date: Feb 2008
Posts: 3
Rep Power: 0 lizi_n is on a distinguished road
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
lizi_n is offline   Reply With Quote
Old Feb 11th, 2008, 3:30 PM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
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
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
random Numbers in openGL csrocker101 C++ 5 Apr 24th, 2007 8:02 PM
Random Numbers crawforddavid2006 C++ 24 Jun 19th, 2006 10:27 PM
random numbers in 2D array cwl157 Java 4 Apr 29th, 2005 6:08 AM
Random Numbers Dark Flare Knight Java 7 Apr 8th, 2005 12:23 PM
random numbers on form load ?_? cloud- Visual Basic 4 Feb 10th, 2005 12:49 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:37 PM.

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