Fortran 95 random_seed() -help

  • Fortran
  • Thread starter Apollion
  • Start date
  • Tags
    Fortran
In summary: Thus, the statement is ill-formed.There is a Fortran function called SECONDS, but it is not used in that way.
  • #1
Apollion
1
0
Hello everone!So i am writing this code where I am producoing random numbers from a generator through a subroutine.Then I call the subroutine and pass them through a Gaussian distribution.I have problem with my seed subroutine: my program compiles but when i try to execute it complain that "Random_seed(): PUT array too small" i have no idea what does this means!

Here is my subroutine:
subroutine setSEED (seed)
implicit none

real*8:: x
integer, dimension(2), intent(inout):: seed
if (seed(1) == 0.0) &
this where
it complains --> seed = floor(1000*secnds(0.0)) +(/0, 37 /)
--> call random_seed(put=seed)

end subroutine setSEED

and my gaussian distribution :

Real*8 Function gasdev(idum)
implicit none
integer, intent(inout) :: idum
integer, save::iset
real*8:: fac,rsq,v1,v2
real*8, dimension(2) :: x
real*8, save :: gset
!data iset/0/
if (idum.lt.0) iset=0
if (iset.eq.0) then
rsq = 0.0
do while (rsq > 1.0.or.rsq==0)
call random_number(x)
v1=2.*x(1)-1
v2=2.*x(2)-1
rsq=v1**2+v2**2

end do

fac=sqrt(-2.*log(rsq)/rsq)
gset=v1*fac
gasdev=v2*fac
iset=1
else
gasdev=gset
iset=0
endif

return

end Function gasdev

...AND MY MAIN PROGRAM:

program mainprog

implicit none

real*8::angle,gasdev,number
integer::i
integer, dimension(2) :: seed = 0

open(299,FILE='gauss.dat',STATUS='REPLACE')

call setSEED(seed)

Do i=1,10,1


angle=gasdev(seed)
print*,"angle=gasdev =",angle
write(299,*),angle


end do

close(299,STATUS="KEEP")
end program mainprog


Do you guys think can help me?its so frustrating!thank you all! :)
 
Last edited:
Technology news on Phys.org
  • #2
Please put [noparse]
Code:
 and
[/noparse] tags around your code. I have done that for you.
Apollion said:
Hello everone!So i am writing this code where I am producoing random numbers from a generator through a subroutine.Then I call the subroutine and pass them through a Gaussian distribution.I have problem with my seed subroutine: my program compiles but when i try to execute it complain that "Random_seed(): PUT array too small" i have no idea what does this means!

Here is my subroutine:
Code:
subroutine setSEED (seed)
implicit none

real*8:: x
integer, dimension(2), intent(inout):: seed
if (seed(1) == 0.0) &
' this where
' it complains
seed = floor(1000*secnds(0.0)) +(/0, 37 /)  
call random_seed(put=seed)    
end subroutine setSEED

and my gaussian distribution :
Code:
Real*8 Function gasdev(idum)        
  implicit none
  integer, intent(inout) :: idum
  integer, save::iset
  real*8:: fac,rsq,v1,v2
  real*8, dimension(2) :: x
  real*8, save :: gset
  !data iset/0/
  if (idum.lt.0) iset=0
  if (iset.eq.0) then
     rsq = 0.0
     do while (rsq > 1.0.or.rsq==0)   
       call random_number(x)
       v1=2.*x(1)-1
       v2=2.*x(2)-1
       rsq=v1**2+v2**2
     end do	
	
     fac=sqrt(-2.*log(rsq)/rsq)
     gset=v1*fac
     gasdev=v2*fac
     iset=1
  else
    gasdev=gset
    iset=0
  endif
  return
end Function gasdev

...AND MY MAIN PROGRAM:

Code:
program mainprog
 
   implicit none
	
   real*8::angle,gasdev,number
   integer::i
   integer, dimension(2) :: seed = 0
	
   open(299,FILE='gauss.dat',STATUS='REPLACE')

   call setSEED(seed)

   Do i=1,10,1
      angle=gasdev(seed)    
      print*,"angle=gasdev =",angle
      write(299,*),angle
   end do
		
   close(299,STATUS="KEEP")
end program mainprog


Do you guys think can help me?its so frustrating!thank you all! :)

What are you trying to do with this code:
Code:
seed = floor(1000*secnds(0.0)) +(/0, 37 /)  
call random_seed(put=seed)

In the first line, floor returns a single value, so it looks like you are trying to add a scalar to a vector - you can't do that.
 
  • #3
Apollion said:
Hello everone!So i am writing this code where I am producoing random numbers from a generator through a subroutine.Then I call the subroutine and pass them through a Gaussian distribution.I have problem with my seed subroutine: my program compiles
</QUOTE>

Are you sure? There appear to be at least two errors in it.
It is necessary to get a clean compilation before attempting to execute
the program.
1. "secnds" is not a known Fortran name. Nor it it defined. What is it?
2. The statement:
write(299,*),angle
in the main program has a useless comma in it.

To get useful advice, you need to post ALL and EVERY error message(s).

<quote>
but when i try to execute it complain that "Random_seed(): PUT array too small" i have no idea what does this means!

Here is my subroutine:
subroutine setSEED (seed)
implicit none

real*8:: x
integer, dimension(2), intent(inout):: seed
if (seed(1) == 0.0) &
this where
it complains --> seed = floor(1000*secnds(0.0)) +(/0, 37 /)
--> call random_seed(put=seed)

end subroutine setSEED

and my gaussian distribution :

Real*8 Function gasdev(idum)
implicit none
integer, intent(inout) :: idum
integer, save::iset
real*8:: fac,rsq,v1,v2
real*8, dimension(2) :: x
real*8, save :: gset
!data iset/0/
if (idum.lt.0) iset=0
if (iset.eq.0) then
rsq = 0.0
do while (rsq > 1.0.or.rsq==0)
call random_number(x)
v1=2.*x(1)-1
v2=2.*x(2)-1
rsq=v1**2+v2**2

end do

fac=sqrt(-2.*log(rsq)/rsq)
gset=v1*fac
gasdev=v2*fac
iset=1
else
gasdev=gset
iset=0
endif

return

end Function gasdev

...AND MY MAIN PROGRAM:

program mainprog

implicit none

real*8::angle,gasdev,number
integer::i
integer, dimension(2) :: seed = 0

open(299,FILE='gauss.dat',STATUS='REPLACE')

call setSEED(seed)

Do i=1,10,1


angle=gasdev(seed)
print*,"angle=gasdev =",angle
write(299,*),angle


end do

close(299,STATUS="KEEP")
end program mainprog
</quote>
 
  • #4
Mark44 said:
What are you trying to do with this code:
Code:
seed = floor(1000*secnds(0.0)) +(/0, 37 /)  
call random_seed(put=seed)

In the first line, floor returns a single value, so it looks like you are trying to add a scalar to a vector - you can't do that.

It's perfectly legal to add a scalar to a vector.
Thus,
seed = 5 + (/ 0, 37 /)
is OK too.
The problem with the line is that "secnds" has not been defined.
 
  • #5


Hello,

I can see that you are using the Fortran 95 random_seed() function to generate random numbers for your program. The error message you are getting, "PUT array too small", means that the array you are passing to the random_seed() function is not large enough to hold the generated random numbers.

In your setSEED subroutine, you are setting the seed to be a two-element array, with one of the elements being set to 0. However, in your main program, you are passing the seed as a single integer value, which is not large enough to hold the generated random numbers.

I would suggest modifying your setSEED subroutine to accept a larger array, such as a four-element array, and then passing that array to the random_seed() function in your main program. This should resolve the "PUT array too small" error.

I hope this helps and good luck with your program!
 

What is the purpose of the random_seed() function in Fortran 95?

The random_seed() function is used to control the initialization of the random number generator in Fortran 95. It allows the user to specify a starting seed value for creating a sequence of pseudo-random numbers.

How do I use the random_seed() function in my Fortran 95 program?

To use the random_seed() function, you must first declare an integer array of size 1 or greater. Then, you can call the function with the array as an argument. This will initialize the random number generator with the values in the array.

Can I use a non-integer array as an argument for random_seed()?

No, the argument for the random_seed() function must be an integer array. If you try to use a non-integer array, the compiler will give you an error.

What happens if I don't call the random_seed() function in my Fortran 95 program?

If you do not call the random_seed() function, the random number generator will use a default seed value. This can result in the same sequence of pseudo-random numbers being generated each time the program is run.

Is the random_seed() function necessary for all Fortran 95 programs that use random numbers?

No, the random_seed() function is only necessary if you want to control the initialization of the random number generator. If you are satisfied with the default seed value, you do not need to use this function.

Similar threads

  • Programming and Computer Science
Replies
4
Views
616
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
5
Views
7K
  • Programming and Computer Science
Replies
2
Views
21K
Back
Top