Random Numbers in Co-array Fortran
Here’s the translation of the random numbers example from Go to Co-array Fortran, formatted in Markdown suitable for Hugo:
Our program demonstrates the generation of random numbers in Co-array Fortran. Here’s the full source code:
In this program, we use Fortran’s intrinsic random_number
subroutine to generate pseudorandom numbers. Here’s a breakdown of the code:
We use
random_number(r)
to generate a random real number between 0.0 and 1.0.To get random integers between 0 and 99, we multiply the random real by 100 and convert to an integer.
We demonstrate generating random floats in different ranges, such as between 5.0 and 10.0.
To set a known seed for reproducible results, we use
random_seed(put=[42, 1024])
. This is similar to creating a new random source with a specific seed in other languages.We show that using the same seed produces the same sequence of random numbers.
To compile and run the program:
Note that the actual numbers generated may be different when you run the sample, except for the last two lines which should be consistent due to the fixed seed.
Fortran’s random number generation is relatively simple compared to some other languages, but it provides the basic functionality needed for many applications. For more advanced random number generation, you might need to use external libraries or implement specific algorithms yourself.