LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 11-16-2011, 11:34 AM   #1
C.L.
LQ Newbie
 
Registered: Jul 2010
Posts: 17

Rep: Reputation: 1
Fortran 90, how do I use random_number and random_seed?


Hello,

I was given a program written by someone else that uses random_seed and random_number to generate a matrix. I thought the output of that program should change because each execution of that program should use a different random number to create the matrix, but the output is always the same, even after I recompile the program.

So I am trying to figure out how random_seed and random_number works in fortran. I'm using the example program given on the random_number page:

Code:
program test_random_number
   REAL(8) :: r
   CALL random_seed()
   CALL RANDOM_NUMBER(r)
   print *, r
end program
This should give me a different number each time I execute it, right? But it isn't. It gives me the same number even after I recompile. I did not specify any arguments for random_seed because the program I was working with did not specify any arguments for random_seed.

What am I missing about how these two subroutines work?
 
Old 11-16-2011, 12:03 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137
The random number generator in many programs does not actually give you a random number, it gives you a pseudorandom SEQUENCE. This means that if it starts the sequence from the same point, it will give you the same sequence every time. For routines like this, I generally initialize them using the current system time (preferably at the microsecond or millisecond level) to give you a "random" starting position for your pseudorandom sequence.
 
1 members found this post helpful.
Old 11-16-2011, 12:08 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137
Here's a real quick and dirty example of how that can be done:

Code:
integer :: values(1:8), k
integer, dimension(:), allocatable :: seed
real(8) :: r

call date_and_time(values=values)

call random_seed(size=k)
allocate(seed(1:k))
seed(:) = values(8)
call random_seed(put=seed)
call random_number(r)
print *,r
Once the sequence is initialized (the "call random_seed(put=seed)" command), you can ignore the seed and just use "call random_number(r)" from then on.
 
1 members found this post helpful.
Old 11-16-2011, 01:12 PM   #4
C.L.
LQ Newbie
 
Registered: Jul 2010
Posts: 17

Original Poster
Rep: Reputation: 1
Thank you both. My problem is solved.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
fortran vivekmadhav007 Programming 4 03-15-2009 07:00 AM
g77 in gcc 4.1.0 not found only gfortran fortran 95 compiler! I need fortran 77. TheBrick Linux - Software 3 07-04-2007 06:39 AM
where is fortran.h? songqsh Programming 8 04-19-2006 05:42 PM
Gnu Fortran versus Intel Fortran tomatoefish Linux - Software 3 02-20-2006 01:31 PM
does linux fortran compiler in fedora 4 support VAX FORTRAN? terrence Programming 17 08-31-2005 08:59 AM

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

All times are GMT -5. The time now is 10:58 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration