Silverfrost Forums

Welcome to our forums

Random number generator

31 May 2010 1:35 #6459

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

31 May 2010 1:57 #6460

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

1 Jun 2010 1:10 #6471

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

3 Jun 2010 6:10 #6494

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

3 Jun 2010 6:31 #6495

I have just run this code and works fine:

program seeds implicit none real8 random real8, 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

4 Jun 2010 11:33 #6501

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

Please login to reply.