LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Help in gfortran programming (https://www.linuxquestions.org/questions/programming-9/help-in-gfortran-programming-4175600835/)

muneer2009 03-01-2017 07:07 AM

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

pan64 03-01-2017 08:04 AM

http://www.linuxquestions.org/questi...gs-4175464257/

suicidaleggroll 03-01-2017 10:12 AM

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?"

muneer2009 03-01-2017 06:07 PM

Quote:

Originally Posted by suicidaleggroll (Post 5677601)
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?"

Accept my appology, I have added my codes now.

suicidaleggroll 03-01-2017 06:17 PM

Why are you declaring randomReal as an integer?
Also you're printing x before you fill it up.

muneer2009 03-01-2017 07:09 PM

Quote:

Originally Posted by suicidaleggroll (Post 5677819)
Why are you declaring randomReal as an integer?
Also you're printing x before you fill it up.

I have declared randomReal to real now and have numbers listed below.

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.

NevemTeve 03-01-2017 11:18 PM

@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.