Menu Close

Generate Random Number – FORTRAN

In Fortran, you can generate random numbers using the RANDOM_NUMBER intrinsic subroutine.

The RANDOM_NUMBER subroutine fills an array with random values between 0 and 1.

 

Example

We generate an array random_numbers of size n and use the random_number subroutine to fill it with random values between 0 and 1.

The random_number subroutine is called with the array as an argument, and it modifies the array with random values.

After generating the random numbers, we use a do loop to iterate through the array and print each random number to the standard output using the write statement.

 

program RandomNumberExample
  implicit none

  integer, parameter :: n = 5  ! Number of random numbers to generate
  real :: random_numbers(n)    ! Array to store random numbers
  integer :: i

  call random_number(random_numbers)

  do i = 1, n
    write(*,*) random_numbers(i)
  end do

end program RandomNumberExample

More Related Stuff