Why is the RAND command in Fortran90 giving constant output in a loop?

  • Fortran
  • Thread starter lth
  • Start date
  • Tags
    Fortran90
In summary: I guess the most portable way to initialize the random number generator would be something likeprogram randomimplicit noneinteger::ireal::xcall random_seed(size=i)allocate(integer::k(i))!initialize k() with system clockcall random_seed(put=k(1:i))deallocate(k)do i=1,10call random_number(x)print*,xenddoendBut this is a huge pain compared to just calling random_seed with no arguments.The other practical thing I've run into is that when calling with an argument (other than SIZE, I think), the seed values created by random_seed a re not necessarily values that could be used as seed values later. So you
  • #1
lth
4
0
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
 
Technology news on Phys.org
  • #2
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:
  • #3
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
 
  • #4
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:
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:
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:
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.
 
  • #5
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
 
  • #6
you could just seed with the system clock.
 
  • #7
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.)
 

What is Fortran90 RAND number?

Fortran90 RAND number is a built-in function in the Fortran90 programming language that generates a random number between 0 and 1.

How do I use Fortran90 RAND number?

To use Fortran90 RAND number, you must first include the "random_number" module in your program. Then, you can call the RAND function to generate a random number between 0 and 1.

Can I specify a range for the random number in Fortran90?

Yes, you can specify a range for the random number by multiplying the result of the RAND function by the desired range and adding the minimum value. For example, if you want a random number between 10 and 20, you can multiply the result of the RAND function by 10 and add 10.

Does Fortran90 RAND number generate the same number every time?

No, Fortran90 RAND number generates a different random number each time it is called. However, the sequence of numbers generated may repeat if the program is run multiple times without resetting the random number generator.

How can I reset the random number generator in Fortran90?

To reset the random number generator in Fortran90, you can use the RANDOM_SEED subroutine. This allows you to set a new initial state for the random number generator, ensuring a different sequence of numbers is generated.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
4
Views
3K
Back
Top