Help in gfortran programming
I am a beginner and need help in writing a small program. I am trying to generate random numbers in the range of [-1, 1] in gfortran in 2D. I know that in order to generate random numbers in the range of -1 and 1, I have to rescale the numbers to [0,2) but I am not getting right answer and also unable to figure out my mistake. Here is the code
program random implicit none real :: rnd real, dimension (10,10) :: x integer :: L, i, m, randomReal L = 10 ! length of sequence do i = 1, L call random_number(rnd) randomReal = 2*rnd-1 print *, "random real = ", randomReal end do write(*,*) (x(L,L), L=1,10) ! assign random numbers to array x call random_number(x) end program random Here is what I get after executing random real = 0 random real = 0 random real = 0 random real = 0 random real = 0 random real = 0 random real = 0 random real = 0 random real = 0 random real = 0 0.00000000E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00 6.72623263E-44 1.66754517E-43 0.00000000E+00 2.14721202E-38 0.00000000E+00 |
|
You just described a problem, but you didn't mention what you've done already and which part of it is giving you problems. Specific answers require specific questions. For example, "Here is an excerpt of a program I've written, I expect it to do X but it's doing Y, here's the necessary debugging info, what am I missing?"
|
Quote:
|
Why are you declaring randomReal as an integer?
Also you're printing x before you fill it up. |
Quote:
random real = 0.995119095 random real = 0.133649349 random real = 0.931830645 random real = 0.495855331 random real = -0.265218258 random real = -3.87262106E-02 random real = -0.852491498 random real = -0.989289641 random real = -0.305837512 random real = -0.315512419 and actually I am trying to figure out how to fill x. I multiply randomReal to x, but it gives me error. Thanks for pointing out my mistake. |
@OP Kindly edit your posts (click on [Edit] button) and add [code] and [/code] tags around source-code.
|
All times are GMT -5. The time now is 01:22 AM. |