Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran90 RAND number help

  1. Apr 17, 2008 #1

    lth

    User Avatar

    Hello,

    In VF, I am trying to fill a meshless grid with nodes from a random number generator. The value of the number is simply between 0 and 1. I am having trouble getting the RAND command in Fortran to change the random numbers through a loop. When I debug, the random number generated in each node is constant. I have looked at the help section for srand, random seed and am certain it has to do with the seed size [integer size, seed(2)] but cannot understand the syntax in help document under random numbers. Anyone see what i am doing wrong? Thank you, Lori

    for example------here is the subprogram,
    INCLUDE 'link_f90_dll.h'
    USE IFPORT
    USE LSARG_INT

    IMPLICIT NONE
    real(4) ranval
    REAL(8), ALLOCATABLE :: X(:),Y(:)
    INTEGER :: N
    N=225
    Allocate (X(N),Y(N))

    !portion to define 1st 55 nodes of X(i) and Y(i)
    !
    CALL RANDOM_NUMBER (ranval)

    do i=56,225
    X(i) = 0.6*ranval
    Y(i) = 1*ranval
    enddo
     
  2. jcsd
  3. Apr 17, 2008 #2

    alphysicist

    User Avatar
    Homework Helper

    Hi lth,

    It looks like you're only calling the random number generator once, and then using that for everything else. To correct the specific problem you're talking about, I think you want it to be


    do i=56,225
    CALL RANDOM_NUMBER (ranval)
    X(i) = 0.6*ranval
    Y(i) = 1*ranval
    enddo

    with the call to the random number generator inside the loop so you get a new random number every time you go to set a value.

    (You might still want to set the seed value, though.)
     
    Last edited: Apr 17, 2008
  4. Apr 17, 2008 #3

    lth

    User Avatar

    Hi Alphysicist,

    I put in the loop and works fine now. Than you very much.
    would you know what setting the seed does to help the program?
    fyi...
    Eventually, I will put a condition on the random generator that if the number is unacceptable for my problem, then throw this number out and create a new random number for that X(i) and Y(i) value.

    Again, thx!
    Lori
     
  5. Apr 17, 2008 #4

    alphysicist

    User Avatar
    Homework Helper

    Hi lth,

    I haven't needed to work with random numbers too often in my work so I don't know much about the inner workings. But here are some practical things:

    If your program is:

    Code (Text):

    program random
    implicit none
    integer::i
    real::x

    do i=1,5
    call random_number(x)
    print*,x
    enddo

    end
     
    you'll get five different numbers but they will be the same five numbers every time you run the program. If you use

    Code (Text):

    program random
    implicit none
    integer::i
    real::x

    call random_seed()
    do i=1,5
    call random_number(x)
    print*,x
    enddo

    end
     
    you get five different numbers every time you run the program. I haven't had a need to use the options to the random_seed for my own work, but one way they can be used is to restart the sequence of numbers while your program is running. To get the same five "random" numbers in a row, you could use

    Code (Text):

    program random
    implicit none
    integer,allocatable,dimension(:)::k
    real::rand
    integer::m
    integer::i

    call random_seed

    call random_seed(size=m)
    print*,"Number of integers used as starting value=",m

    Allocate(k(m))

    call random_seed(get=k(1:m))    !this gets the current integers used as a starting value

    print*;print*,"Integers used as starting value."
    print*,k    

    call random_seed(put=k(1:m) )  !setting starting seed value

    print*;print*,"This gives five random values."
    do i=1,5
    call random_number(rand)
    print*,rand
    enddo

    call random_seed(put=k(1:m) ) !resetting the generator to the previous state


    print*
    print*,"The same five 'random' numbers."
    do i=1,5
    call random_number(rand)
    print*,rand
    enddo

    deallocate(k)
    end
     
    The part that confused me personally for a while was that the starting value for the seed is not a (necessarily) a single number but is a set of numbers; for my system it is four integers. That's the point of calling random_seed with the SIZE keyword: to find out how many integers the generator is using; that way I could make the k matrix be of the right size to get and store the starting values.
     
  6. Apr 17, 2008 #5

    lth

    User Avatar

    Hi Alphysicist,

    All I can say is thanks so much for clarity on this. Fortran programming is pretty new to me and this was a big help on my ability to program and run the meshless random grid technique properly.

    Repectfully, LTH
     
  7. Apr 19, 2008 #6
    you could just seed with the system clock.
     
  8. Apr 20, 2008 #7

    alphysicist

    User Avatar
    Homework Helper

    Hi ice109,

    For the compilers I'm using I believe that's what calling random_seed with no argument does--it seeds with values based on the system clock.

    To make a program completely portable I guess it would be necessary to write in the steps to send random_seed a system clock value. But that would take quite a few extra lines to check dimensions, allocate arrays or get parts of arrays, etc.

    I'm fairly certain there are compilers where calling random_seed with no argument does not lead to different values (like gfortran or the intel compiler? I've haven't used them personally but I think someone mentioned they act that way.)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Fortran90 RAND number help
  1. Fortran90 help (Replies: 4)

  2. Help me in fortran90 (Replies: 7)

  3. Fortran90 help (Replies: 5)

Loading...