mercredi 21 avril 2021

How to Create Random Integer Array of Specified Length and Save as Data File in Fortran

I have two questions here. First, I would like to know how to make my random integer array in Fortran a specified length and range. So far I can only seem to find codes where the length and the range are the same. Here is the code that I have:

program rand_test
use, intrinsic :: ISO_Fortran_env

    real(REAL32) :: r(5)
    integer ::i(5)

    call random_seed()
    call random_number(r)

    i = floor( r*6._REAL32 )

    print *, i

end program

When I run this, it prints out: 5 3 5 4 2. So it gives me 5 random numbers on an interval of 1-5. However, I want to be able to modify this so that I can have something like 10 random numbers that are all on that interval of 1-5.

The second thing I want to do is replace the print function with a code that will save the array to a data file. I have another code that created a data file successfully but for some reason when I combine it with this one it does not work properly. This is the full other code:

program outputdata   
implicit none

   real, dimension(100) :: x, y  
   real, dimension(100) :: p, q
   integer :: i  

   ! data  
   do i=1,100  
      x(i) = i * 0.1 
      y(i) = sin(x(i)) * (1-cos(x(i)/3.0))  
   end do  

   ! output data into a file 
   open(1, file = 'data1.dat', status = 'new')  
   do i=1,100  
      write(1,*) x(i), y(i)   
   end do  

   close(1) 

end program outputdata

This program works exactly like I want it to, outputting 100 x values in one column and 100 y values into another in the file data1.dat. I attempted to take the last part of the code and modify and combine it with my random number generator by adding this to the end of my first code:

  ! output data into a file 
   open(1, file = 'data1.dat', status = 'new')  
   do 
      write(1,*) i   
   end do  

   close(1) 

When I do this, however, it repeats the 5 numbers instead of just writing them once. I also attempted a completely different version of this that I thought looked more like the original code I used to practice making data files:

module random_mod
implicit none
contains

    function getRandInt(lowerBound,upperBound) result(randInt)
        use iso_fortran_env, only: RK => real64
        implicit none
        integer, intent(in) :: lowerBound,upperBound
        real                :: dummy
        integer             :: randInt
        call random_number(dummy)
        randInt = lowerBound + floor(dummy*real(upperBound-lowerBound+1))
    end function getRandInt

end module random_mod



program outputdata   
use random_mod, only: getRandInt
implicit none

    real, dimension(100) :: x
    real, dimension(100) :: p
    integer :: i

    do i = 1, 100
        x(i) = getRandInt(-100,100)
    end do

    ! output data into a file 
    open(1, file = 'data_particle4.dat', status = 'new')  
        do i=1, 100
            write(1,*) x
        end do
    close(1) 

end program outputdata

This has the same issue, though. It gives me 100 random numbers from -100 to 100 but then keeps repeating that same array. I don't understand why this code has the same problem because to me this outputdata program looks practically identical to the first one I used that worked perfectly.




Aucun commentaire:

Enregistrer un commentaire