![]() |
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 |
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 indexIn Pseudocode: :
n = 10 |
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! |
Re: random numbers fortran 90
This is what I've got so far if it helps.
:
integer:: n |
Re: random numbers fortran 90
:
x(i)=x(j)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.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) |
| 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