23.3: Example
- Page ID
- 54397
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)
A simple example program to generate 100 random integer numbers, each between 1 and 100, is as follows:
program rand implicit none integer, parameter :: rcount=100 integer :: i integer, dimension(rcount) :: nums real :: x call random_seed() do i = 1, rcount call random_number(x) nums(i) = int(x*100.0) + 1 end do write (*,'(a)') "Random Numbers:" do i = 1, rcount write (*,'(i3,2x)', advance="no") nums(i) if (mod(i,10)==0) write (*,*) end do end program rand
The call to random_seed()
must be performed before the call to random_number()
. Additionally, the call to random_seed()
can only be performed once.
The output of this example program is shown below:
Random Numbers: 100 57 97 75 37 49 8 1 35 35 22 14 91 39 45 67 2 66 65 33 86 41 21 97 60 68 46 34 11 76 61 72 90 66 16 62 98 100 26 56 66 56 98 91 66 73 41 93 15 68 77 34 12 62 83 95 74 50 38 43 56 100 100 75 96 10 74 76 95 71 82 56 7 49 60 14 59 52 89 31 67 67 51 27 8 11 55 38 2 80 63 78 96 12 32 60 5 12 22 11
Each execution will generate the same series of random numbers.