![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2008
Posts: 3
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
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 |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2008
Posts: 3
Rep Power: 0
![]() |
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! |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Feb 2008
Posts: 3
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |