LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-01-2017, 07:07 AM   #1
muneer2009
LQ Newbie
 
Registered: Mar 2017
Posts: 3

Rep: Reputation: Disabled
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

Last edited by muneer2009; 03-01-2017 at 06:04 PM. Reason: Added my code
 
Old 03-01-2017, 08:04 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
http://www.linuxquestions.org/questi...gs-4175464257/
 
1 members found this post helpful.
Old 03-01-2017, 10:12 AM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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.
 
1 members found this post helpful.
Old 03-01-2017, 06:07 PM   #4
muneer2009
LQ Newbie
 
Registered: Mar 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
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.
 
Old 03-01-2017, 06:17 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

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

Last edited by suicidaleggroll; 03-01-2017 at 06:19 PM.
 
Old 03-01-2017, 07:09 PM   #6
muneer2009
LQ Newbie
 
Registered: Mar 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
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.
 
Old 03-01-2017, 11:18 PM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
@OP Kindly edit your posts (click on [Edit] button) and add [code] and [/code] tags around source-code.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] What does it mean in GFortran? AlexBB Linux - Newbie 7 12-16-2014 08:28 PM
[SOLVED] What does it mean in GFortran? AlexBB Linux - Newbie 4 12-13-2014 12:43 PM
gfortran for SLES 11 mahdi109 Linux - Software 3 06-03-2012 10:39 AM
help gfortran mperez Programming 2 12-16-2007 12:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration