ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
Last edited by muneer2009; 03-01-2017 at 06:04 PM.
Reason: Added my code
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?"
Last edited by suicidaleggroll; 03-01-2017 at 10:15 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?"
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.