Next
/
Previous
/ Index
/ TCC Help System
/ Publications
/ Site map
/ NM Tech homepage
Scaling random numbers in Fortran
|
|
The Fortran rand() always returns a number between 0 and
1. More specifically, it is a number in the half-open interval [0,1),
so you might get a 0.0 result, but the result will never be exactly
1.0 or greater.
Such a number can be scaled to produce uniformly distributed
pseudorandom numbers in any interval.
- If you want an integer between i and j inclusive,
use the expression:
int(rand(0)*(j+1-i))+i
For example, to get integers from 1 to 4, use:
int(rand(0)*4)+1
- If you want a real number in the interval [x,y),
use this expression:
(rand(0)*(y-x))+x
For example, to get numbers in the range [10,20), use:
(rand(0)*10.0)+10.0
Next: A sample random number Fortran program
See also: Generating random numbers in Fortran; Hints for Fortran programmers
Previous: Non-repeating pseudorandom sequences in Fortran
Site map
Index:
Keyword index
to help pages
Help:
New Mexico Tech Computer Center: Help System
TCC Publications
Home:
About New Mexico Tech
John Shipman, john@nmt.edu
Last updated: 1995/12/19 18:58:09 UT
URL: http://www.nmt.edu/tcc/help/lang/fortran/scaling.html