forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Random number generator

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
tamara



Joined: 28 May 2010
Posts: 4

PostPosted: Mon May 31, 2010 2:35 pm    Post subject: Random number generator Reply with quote

Hi All,

sorry I'm a bit new and inexperienced at programming in fortran. I have seen in the documentation that there is a random number generator incorporated with this compiler but I'm unable to come up with the correct way of calling this function from a programme I'm writing.

I was using

random = FUNCTION RANDOM@()

Where random is a declared real variable. Do I have to put something else to incorporate a library of functions at the beginning of the code?

Any suggestions?

Thanks
Tamara
Back to top
View user's profile Send private message
aebolzan



Joined: 06 Jul 2007
Posts: 229
Location: La Plata, Argentina

PostPosted: Mon May 31, 2010 2:57 pm    Post subject: Reply with quote

Hello Tamara,

you should put something like...

subroutine my_sub
real*8 :: random,number
.......
number=random()
........
end subroutine my_sub

I wouldn't use random = random() as it could be rather confusing to use the name of a function as the name of one of my variables

Best regards,

Agustin
Back to top
View user's profile Send private message
Wilfried Linder



Joined: 14 Nov 2007
Posts: 314
Location: Düsseldorf, Germany

PostPosted: Tue Jun 01, 2010 2:10 pm    Post subject: Reply with quote

To make the numbers a bit more random it is a good idea to initialise the generator with "call date_time_seed@()" before using random().

Regards, Wilfried
Back to top
View user's profile Send private message
tamara



Joined: 28 May 2010
Posts: 4

PostPosted: Thu Jun 03, 2010 7:10 pm    Post subject: Reply with quote

Thanks a lot for all the prompt answers - this is great and makes it easy.

However when I write what I think is the code aebolzan suggested I get the following error message when I try to compile the model:
'RANDOM is a routine name and cannot be used in this context'

Maybe I should add that the call to the random function is occuring in a loop statement

Thanks a lot
Tamara
Back to top
View user's profile Send private message
aebolzan



Joined: 06 Jul 2007
Posts: 229
Location: La Plata, Argentina

PostPosted: Thu Jun 03, 2010 7:31 pm    Post subject: Reply with quote

I have just run this code and works fine:

program seeds
implicit none
real*8 random
real*8, dimension(:), allocatable ::seed
integer i
allocate (seed(100))
do i=1,100
seed(i) = random()*100.0
end do
print*,seed
end program seeds

I do not know how is your code....

Best regards,

Agustin
Back to top
View user's profile Send private message
tamara



Joined: 28 May 2010
Posts: 4

PostPosted: Fri Jun 04, 2010 12:33 pm    Post subject: Reply with quote

Thanks a lot - yes, that fixes the problem. I made a mistake when updating my code - I changed the variable name from 'random' to 'number' for the random number generator, but then did not change it later on in the programme. Sorry about the confusion.

Tamara
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group