Random numbers

Contents
Seed once
Scalar draws
Fill arrays
Normal via Box-Muller

Seed once

Seed the generator once at startup so runs differ or repeat on demand. Ask random_seed for the seed size, fill an array with your seed, then put it back. Never run the seeding code before every draw.

program seed_demo implicit none integer :: n integer, allocatable :: seed(:) call random_seed(size=n) allocate(seed(n)) seed = 20261006 call random_seed(put=seed) end program seed_demo

Scalar draws

Random_number fills a scalar or an array with uniform values from 0 up to but excluding 1. One call gives one draw for a scalar variable.

program scalar_demo implicit none real :: x call random_number(x) print *, x end program scalar_demo

Fill arrays

Pass a whole array to fill it with one call. Scale and shift the result to move it into the range you need. The code below maps the unit interval onto 5 to 15.

program array_demo implicit none real :: a(5) call random_number(a) ! map unit values onto 5 to 15 a = 5.0 + 10.0 * a print *, a end program array_demo

Normal via Box-Muller

Uniform draws become normal draws with the Box-Muller transform. Draw two uniforms, then combine them with log, sqrt, and cos. The result has mean 0 and standard deviation 1. Redraw u1 when it is exactly zero so log never sees zero.

program normal_demo implicit none real :: u1, u2, z real, parameter :: pi = 3.14159265 call random_number(u1) call random_number(u2) z = sqrt(-2.0 * log(u1)) * cos(2.0 * pi * u2) print *, z end program normal_demo

Next: String tools

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: