View previous topic :: View next topic |
Author |
Message |
tamara
Joined: 28 May 2010 Posts: 4
|
Posted: Mon May 31, 2010 2:35 pm Post subject: Random number generator |
|
|
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 |
|
|
aebolzan
Joined: 06 Jul 2007 Posts: 229 Location: La Plata, Argentina
|
Posted: Mon May 31, 2010 2:57 pm Post subject: |
|
|
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 |
|
|
Wilfried Linder
Joined: 14 Nov 2007 Posts: 314 Location: Düsseldorf, Germany
|
Posted: Tue Jun 01, 2010 2:10 pm Post subject: |
|
|
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 |
|
|
tamara
Joined: 28 May 2010 Posts: 4
|
Posted: Thu Jun 03, 2010 7:10 pm Post subject: |
|
|
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 |
|
|
aebolzan
Joined: 06 Jul 2007 Posts: 229 Location: La Plata, Argentina
|
Posted: Thu Jun 03, 2010 7:31 pm Post subject: |
|
|
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 |
|
|
tamara
Joined: 28 May 2010 Posts: 4
|
Posted: Fri Jun 04, 2010 12:33 pm Post subject: |
|
|
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 |
|
|
|